-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4deed68
commit 39a998e
Showing
31 changed files
with
1,221 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2023 Harness, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http:https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package repo | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
apiauth "github.com/harness/gitness/app/api/auth" | ||
"github.com/harness/gitness/app/api/usererror" | ||
"github.com/harness/gitness/app/auth" | ||
"github.com/harness/gitness/app/auth/authz" | ||
"github.com/harness/gitness/app/store" | ||
"github.com/harness/gitness/types" | ||
"github.com/harness/gitness/types/enum" | ||
) | ||
|
||
// GetRepo fetches an active repo (not one that is currently being imported). | ||
func GetRepo( | ||
ctx context.Context, | ||
repoStore store.RepoStore, | ||
repoRef string, | ||
) (*types.Repository, error) { | ||
if repoRef == "" { | ||
return nil, usererror.BadRequest("A valid repository reference must be provided.") | ||
} | ||
|
||
repo, err := repoStore.FindByRef(ctx, repoRef) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to find repository: %w", err) | ||
} | ||
|
||
if repo.Importing { | ||
return nil, usererror.BadRequest("Repository import is in progress.") | ||
} | ||
|
||
return repo, nil | ||
} | ||
|
||
// GetRepoCheckAccess fetches an active repo (not one that is currently being imported) | ||
// and checks if the current user has permission to access it. | ||
func GetRepoCheckAccess( | ||
ctx context.Context, | ||
repoStore store.RepoStore, | ||
authorizer authz.Authorizer, | ||
session *auth.Session, | ||
repoRef string, | ||
reqPermission enum.Permission, | ||
orPublic bool, | ||
) (*types.Repository, error) { | ||
repo, err := GetRepo(ctx, repoStore, repoRef) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to find repo: %w", err) | ||
} | ||
|
||
if err = apiauth.CheckRepo(ctx, authorizer, session, repo, reqPermission, orPublic); err != nil { | ||
return nil, fmt.Errorf("access check failed: %w", err) | ||
} | ||
|
||
return repo, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2023 Harness, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http:https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package reposettings | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/harness/gitness/app/api/controller/repo" | ||
"github.com/harness/gitness/app/auth" | ||
"github.com/harness/gitness/app/auth/authz" | ||
"github.com/harness/gitness/app/services/settings" | ||
"github.com/harness/gitness/app/store" | ||
"github.com/harness/gitness/types" | ||
"github.com/harness/gitness/types/enum" | ||
) | ||
|
||
type Controller struct { | ||
authorizer authz.Authorizer | ||
repoStore store.RepoStore | ||
settings *settings.Service | ||
} | ||
|
||
func NewController( | ||
authorizer authz.Authorizer, | ||
repoStore store.RepoStore, | ||
settings *settings.Service, | ||
) *Controller { | ||
return &Controller{ | ||
authorizer: authorizer, | ||
repoStore: repoStore, | ||
settings: settings, | ||
} | ||
} | ||
|
||
// getRepoCheckAccess fetches an active repo (not one that is currently being imported) | ||
// and checks if the current user has permission to access it. | ||
func (c *Controller) getRepoCheckAccess( | ||
ctx context.Context, | ||
session *auth.Session, | ||
repoRef string, | ||
reqPermission enum.Permission, | ||
orPublic bool, | ||
) (*types.Repository, error) { | ||
return repo.GetRepoCheckAccess( | ||
ctx, | ||
c.repoStore, | ||
c.authorizer, | ||
session, | ||
repoRef, | ||
reqPermission, | ||
orPublic, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2023 Harness, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http:https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package reposettings | ||
|
||
import ( | ||
"github.com/harness/gitness/app/services/settings" | ||
|
||
"github.com/gotidy/ptr" | ||
) | ||
|
||
// SecuritySettings represents the security related part of repository settings as exposed externally. | ||
type SecuritySettings struct { | ||
SecretScanningEnabled *bool `json:"secret_scanning_enabled"` | ||
} | ||
|
||
func GetDefaultSecuritySettings() *SecuritySettings { | ||
return &SecuritySettings{ | ||
SecretScanningEnabled: ptr.Bool(settings.DefaultSecretScanningEnabled), | ||
} | ||
} | ||
|
||
func GetSecuritySettingsMappings(s *SecuritySettings) []settings.SettingHandler { | ||
return []settings.SettingHandler{ | ||
settings.Mapping(settings.KeySecretScanningEnabled, s.SecretScanningEnabled), | ||
} | ||
} | ||
|
||
func GetSecuritySettingsAsKeyValues(s *SecuritySettings) []settings.KeyValue { | ||
kvs := make([]settings.KeyValue, 0, 1) | ||
if s.SecretScanningEnabled != nil { | ||
kvs = append(kvs, settings.KeyValue{Key: settings.KeySecretScanningEnabled, Value: *s.SecretScanningEnabled}) | ||
} | ||
return kvs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2023 Harness, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http:https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package reposettings | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/harness/gitness/app/auth" | ||
"github.com/harness/gitness/types/enum" | ||
) | ||
|
||
// SecurityFind returns the security settings of a repo. | ||
func (c *Controller) SecurityFind( | ||
ctx context.Context, | ||
session *auth.Session, | ||
repoRef string, | ||
) (*SecuritySettings, error) { | ||
repo, err := c.getRepoCheckAccess(ctx, session, repoRef, enum.PermissionRepoView, true) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
out := GetDefaultSecuritySettings() | ||
mappings := GetSecuritySettingsMappings(out) | ||
err = c.settings.RepoMap(ctx, repo.ID, mappings...) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to map settings: %w", err) | ||
} | ||
|
||
return out, nil | ||
} |
Oops, something went wrong.