Skip to content

Commit

Permalink
fix lint errors
Browse files Browse the repository at this point in the history
Signed-off-by: Arvindh <arvindh91@gmail.com>
  • Loading branch information
arvindh123 committed Jul 10, 2024
1 parent a6ae7df commit ee226b7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 319 deletions.
2 changes: 1 addition & 1 deletion auth/api/http/pats/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,6 @@ func testCheckPATScopeEntryEndpoint(svc auth.Service) endpoint.Endpoint {
return nil, err
}

return revokePatSecretRes{}, nil
return testCheckPatScopeRes{}, nil
}
}
289 changes: 0 additions & 289 deletions auth/bolt/pat.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ var (
entityValue = []byte{0x02}
anyIDValue = []byte{0x03}
selectedIDsValue = []byte{0x04}

errBucketNotFound = errors.New("bucket not found")
)

type patRepo struct {
Expand Down Expand Up @@ -525,269 +523,6 @@ func keyValueToPAT(kv map[string][]byte) (auth.PAT, error) {
return pat, nil
}

// 129eab11-681b-4c62-89e4-7a9ce0eda29d:scope:users:read
// 129eab11-681b-4c62-89e4-7a9ce0eda29d:scope:domains:domain_1:groups:read
// 129eab11-681b-4c62-89e4-7a9ce0eda29d:scope:domains:domain_1:groups:read:group_2
// 129eab11-681b-4c62-89e4-7a9ce0eda29d:scope:domains:domain_1:domain_management:read
// 129eab11-681b-4c62-89e4-7a9ce0eda29d:scope:domains:domain_1:groups:read:group_1

func parseKeyValueToScopeLegacy(kv map[string][]byte) (auth.Scope, error) {
scope := auth.Scope{
Domains: make(map[string]auth.DomainScope),
}
for key, value := range kv {
if strings.Index(key, keySeparator+scopeKey+keySeparator) > 0 {
keyParts := strings.Split(key, keySeparator)

platformEntityType, err := auth.ParsePlatformEntityType(keyParts[2])
if err != nil {
return auth.Scope{}, errors.Wrap(repoerr.ErrViewEntity, err)
}

switch platformEntityType {
case auth.PlatformUsersScope:
switch string(value) {
case string(entityValue):
if len(keyParts) != 5 {
return auth.Scope{}, fmt.Errorf("invalid scope key format: %s", key)
}
opType, err := auth.ParseOperationType(keyParts[len(keyParts)-2])
if err != nil {
return auth.Scope{}, errors.Wrap(repoerr.ErrViewEntity, err)
}
entityID := keyParts[len(keyParts)-1]
if scope.Users.Operations == nil {
scope.Users.Operations = make(map[auth.OperationType]auth.ScopeValue)
}

if _, oValueExists := scope.Users.Operations[opType]; !oValueExists {
scope.Users.Operations[opType] = &auth.SelectedIDs{}
}
oValue := scope.Users.Operations[opType]
if err := oValue.AddValues(entityID); err != nil {
return auth.Scope{}, fmt.Errorf("failed to add scope key %s with entity value %v : %w", key, entityID, err)
}
scope.Users.Operations[opType] = oValue
case string(anyIDValue):
if len(keyParts) != 4 {
return auth.Scope{}, fmt.Errorf("invalid scope key format: %s", key)
}
opType, err := auth.ParseOperationType(keyParts[len(keyParts)-1])
if err != nil {
return auth.Scope{}, errors.Wrap(repoerr.ErrViewEntity, err)
}

if scope.Users.Operations == nil {
scope.Users.Operations = make(map[auth.OperationType]auth.ScopeValue)
}

if oValue, oValueExists := scope.Users.Operations[opType]; oValueExists && oValue != nil {
if _, ok := oValue.(*auth.AnyIDs); !ok {
return auth.Scope{}, fmt.Errorf("failed to add scope key %s with entity anyIDs scope value : key already initialized with different type", key)
}
}
scope.Users.Operations[opType] = &auth.AnyIDs{}
case string(selectedIDsValue):
if len(keyParts) != 4 {
return auth.Scope{}, fmt.Errorf("invalid scope key format: %s", key)
}
opType, err := auth.ParseOperationType(keyParts[len(keyParts)-1])
if err != nil {
return auth.Scope{}, errors.Wrap(repoerr.ErrViewEntity, err)
}

if scope.Users.Operations == nil {
scope.Users.Operations = make(map[auth.OperationType]auth.ScopeValue)
}

oValue, oValueExists := scope.Users.Operations[opType]
if oValueExists && oValue != nil {
if _, ok := oValue.(*auth.SelectedIDs); !ok {
return auth.Scope{}, fmt.Errorf("failed to add scope key %s with entity selectedIDs scope value : key already initialized with different type", key)
}
}
if !oValueExists {
scope.Users.Operations[opType] = &auth.SelectedIDs{}
}
default:
return auth.Scope{}, fmt.Errorf("key %s have invalid value %v", key, value)
}

case auth.PlatformDomainsScope:
if len(keyParts) < 6 {
return auth.Scope{}, fmt.Errorf("invalid scope key format: %s", key)
}
domainID := keyParts[3]
if scope.Domains == nil {
scope.Domains = make(map[string]auth.DomainScope)
}
if _, ok := scope.Domains[domainID]; !ok {
scope.Domains[domainID] = auth.DomainScope{}
}
domainScope := scope.Domains[domainID]

entityType := keyParts[4]

switch entityType {
case auth.DomainManagementScope.String():
switch string(value) {
case string(entityValue):
if len(keyParts) != 7 {
return auth.Scope{}, fmt.Errorf("invalid scope key format: %s", key)
}
opType, err := auth.ParseOperationType(keyParts[len(keyParts)-2])
if err != nil {
return auth.Scope{}, errors.Wrap(repoerr.ErrViewEntity, err)
}
entityID := keyParts[len(keyParts)-1]

if domainScope.DomainManagement.Operations == nil {
domainScope.DomainManagement.Operations = make(map[auth.OperationType]auth.ScopeValue)
}

if _, oValueExists := domainScope.DomainManagement.Operations[opType]; !oValueExists {
domainScope.DomainManagement.Operations[opType] = &auth.SelectedIDs{}
}
oValue := domainScope.DomainManagement.Operations[opType]
if err := oValue.AddValues(entityID); err != nil {
return auth.Scope{}, fmt.Errorf("failed to add scope key %s with entity value %v : %w", key, entityID, err)
}
domainScope.DomainManagement.Operations[opType] = oValue
case string(anyIDValue):
if len(keyParts) != 6 {
return auth.Scope{}, fmt.Errorf("invalid scope key format: %s", key)
}
opType, err := auth.ParseOperationType(keyParts[len(keyParts)-1])
if err != nil {
return auth.Scope{}, errors.Wrap(repoerr.ErrViewEntity, err)
}

if domainScope.DomainManagement.Operations == nil {
domainScope.DomainManagement.Operations = make(map[auth.OperationType]auth.ScopeValue)
}

if oValue, oValueExists := domainScope.DomainManagement.Operations[opType]; oValueExists && oValue != nil {
if _, ok := oValue.(*auth.AnyIDs); !ok {
return auth.Scope{}, fmt.Errorf("failed to add scope key %s with entity anyIDs scope value : key already initialized with different type", key)
}
}
domainScope.DomainManagement.Operations[opType] = &auth.AnyIDs{}
case string(selectedIDsValue):
if len(keyParts) != 6 {
return auth.Scope{}, fmt.Errorf("invalid scope key format: %s", key)
}
opType, err := auth.ParseOperationType(keyParts[len(keyParts)-1])
if err != nil {
return auth.Scope{}, errors.Wrap(repoerr.ErrViewEntity, err)
}

if domainScope.DomainManagement.Operations == nil {
domainScope.DomainManagement.Operations = make(map[auth.OperationType]auth.ScopeValue)
}

oValue, oValueExists := domainScope.DomainManagement.Operations[opType]
if oValueExists && oValue != nil {
if _, ok := oValue.(*auth.SelectedIDs); !ok {
return auth.Scope{}, fmt.Errorf("failed to add scope key %s with entity selectedIDs scope value : key already initialized with different type", key)
}
}
if !oValueExists {
domainScope.DomainManagement.Operations[opType] = &auth.SelectedIDs{}
}
default:
return auth.Scope{}, fmt.Errorf("key %s have invalid value %v", key, value)
}
default:
etype, err := auth.ParseDomainEntityType(entityType)
if err != nil {
return auth.Scope{}, fmt.Errorf("key %s invalid entity type %s : %w", key, entityType, err)
}
if domainScope.Entities == nil {
domainScope.Entities = make(map[auth.DomainEntityType]auth.OperationScope)
}
if _, ok := domainScope.Entities[etype]; !ok {
domainScope.Entities[etype] = auth.OperationScope{}
}
entityOperationScope := domainScope.Entities[etype]
switch string(value) {
case string(entityValue):
if len(keyParts) != 7 {
return auth.Scope{}, fmt.Errorf("invalid scope key format: %s", key)
}
opType, err := auth.ParseOperationType(keyParts[len(keyParts)-2])
if err != nil {
return auth.Scope{}, errors.Wrap(repoerr.ErrViewEntity, err)
}
entityID := keyParts[len(keyParts)-1]
if entityOperationScope.Operations == nil {
entityOperationScope.Operations = make(map[auth.OperationType]auth.ScopeValue)
}

if _, oValueExists := entityOperationScope.Operations[opType]; !oValueExists {
entityOperationScope.Operations[opType] = &auth.SelectedIDs{}
}
oValue := entityOperationScope.Operations[opType]
if err := oValue.AddValues(entityID); err != nil {
return auth.Scope{}, fmt.Errorf("failed to add scope key %s with entity value %v : %w", key, entityID, err)
}
entityOperationScope.Operations[opType] = oValue
case string(anyIDValue):
if len(keyParts) != 6 {
return auth.Scope{}, fmt.Errorf("invalid scope key format: %s", key)
}
opType, err := auth.ParseOperationType(keyParts[len(keyParts)-1])
if err != nil {
return auth.Scope{}, errors.Wrap(repoerr.ErrViewEntity, err)
}

if entityOperationScope.Operations == nil {
entityOperationScope.Operations = make(map[auth.OperationType]auth.ScopeValue)
}

if oValue, oValueExists := entityOperationScope.Operations[opType]; oValueExists && oValue != nil {
if _, ok := oValue.(*auth.AnyIDs); !ok {
return auth.Scope{}, fmt.Errorf("failed to add scope key %s with entity anyIDs scope value : key already initialized with different type", key)
}
}
entityOperationScope.Operations[opType] = &auth.AnyIDs{}
case string(selectedIDsValue):
if len(keyParts) != 6 {
return auth.Scope{}, fmt.Errorf("invalid scope key format: %s", key)
}
opType, err := auth.ParseOperationType(keyParts[len(keyParts)-1])
if err != nil {
return auth.Scope{}, errors.Wrap(repoerr.ErrViewEntity, err)
}

if entityOperationScope.Operations == nil {
entityOperationScope.Operations = make(map[auth.OperationType]auth.ScopeValue)
}

oValue, oValueExists := entityOperationScope.Operations[opType]
if oValueExists && oValue != nil {
if _, ok := oValue.(*auth.SelectedIDs); !ok {
return auth.Scope{}, fmt.Errorf("failed to add scope key %s with entity selectedIDs scope value : key already initialized with different type", key)
}
}
if !oValueExists {
entityOperationScope.Operations[opType] = &auth.SelectedIDs{}
}
default:
return auth.Scope{}, fmt.Errorf("key %s have invalid value %v", key, value)
}

domainScope.Entities[etype] = entityOperationScope
}

scope.Domains[domainID] = domainScope
default:
return auth.Scope{}, errors.Wrap(repoerr.ErrViewEntity, fmt.Errorf("invalid platform entity type : %s", platformEntityType.String()))
}
}
}
return scope, nil
}

func parseKeyValueToScope(kv map[string][]byte) (auth.Scope, error) {
scope := auth.Scope{
Domains: make(map[string]auth.DomainScope),
Expand Down Expand Up @@ -942,30 +677,6 @@ func validateOperation(platformEntityType auth.PlatformEntityType, opScope auth.
return nil
}

func getString(b *bolt.Bucket, patID, key string) string {
value := b.Get([]byte(patID + keySeparator + key))
if value != nil {
return string(value)
}
return ""
}

func getTime(b *bolt.Bucket, patID, key string) time.Time {
value := b.Get([]byte(patID + keySeparator + key))
if value != nil {
return bytesToTime(value)
}
return time.Time{}
}

func getBool(b *bolt.Bucket, patID, key string) bool {
value := b.Get([]byte(patID + keySeparator + key))
if value != nil {
return bytesToBoolean(value)
}
return false
}

func timeToBytes(t time.Time) []byte {
timeBytes := make([]byte, 8)
binary.BigEndian.PutUint64(timeBytes, uint64(t.Nanosecond()))
Expand Down
Loading

0 comments on commit ee226b7

Please sign in to comment.