forked from raystack/frontier
-
Notifications
You must be signed in to change notification settings - Fork 0
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
cf4b97f
commit 5ca455c
Showing
34 changed files
with
2,579 additions
and
713 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
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,8 @@ | ||
package servicedata | ||
|
||
import "errors" | ||
|
||
var ( | ||
ErrInvalidDetail = errors.New("invalid service data detail") | ||
ErrConflict = errors.New("key already exist") | ||
) |
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,110 @@ | ||
package servicedata | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/goto/shield/core/project" | ||
"github.com/goto/shield/core/relation" | ||
"github.com/goto/shield/core/resource" | ||
"github.com/goto/shield/core/user" | ||
"github.com/goto/shield/internal/schema" | ||
"github.com/goto/shield/pkg/uuid" | ||
) | ||
|
||
const keyNamespace = "shield/servicedata_key" | ||
|
||
type ResourceService interface { | ||
Create(ctx context.Context, res resource.Resource) (resource.Resource, error) | ||
} | ||
|
||
type RelationService interface { | ||
Create(ctx context.Context, rel relation.RelationV2) (relation.RelationV2, error) | ||
} | ||
|
||
type ProjectService interface { | ||
Get(ctx context.Context, idOrSlug string) (project.Project, error) | ||
} | ||
|
||
type UserService interface { | ||
FetchCurrentUser(ctx context.Context) (user.User, error) | ||
} | ||
|
||
type Service struct { | ||
repository Repository | ||
resourceService ResourceService | ||
relationService RelationService | ||
projectService ProjectService | ||
userService UserService | ||
} | ||
|
||
func NewService(repository Repository, resourceService ResourceService, relationService RelationService, projectService ProjectService, userService UserService) *Service { | ||
return &Service{ | ||
repository: repository, | ||
resourceService: resourceService, | ||
relationService: relationService, | ||
projectService: projectService, | ||
userService: userService, | ||
} | ||
} | ||
|
||
func (s Service) CreateKey(ctx context.Context, key Key) (Key, error) { | ||
// check if key contains ':' | ||
if key.Key == "" { | ||
return Key{}, ErrInvalidDetail | ||
} | ||
|
||
// fetch current user | ||
currentUser, err := s.userService.FetchCurrentUser(ctx) | ||
if err != nil { | ||
return Key{}, fmt.Errorf("%w: %s", user.ErrInvalidEmail, err.Error()) | ||
} | ||
|
||
// convert project slug to project id | ||
if !uuid.IsValid(key.ProjectID) { | ||
project, err := s.projectService.Get(ctx, key.ProjectID) | ||
if err != nil { | ||
return Key{}, err | ||
} | ||
key.ProjectID = project.ID | ||
} | ||
|
||
// create URN | ||
key.URN = key.CreateURN() | ||
|
||
// insert the service data key | ||
resource, err := s.resourceService.Create(ctx, resource.Resource{ | ||
Name: key.URN, | ||
NamespaceID: keyNamespace, | ||
ProjectID: key.ProjectID, | ||
UserID: currentUser.ID, | ||
}) | ||
if err != nil { | ||
return Key{}, err | ||
} | ||
key.ResourceID = resource.Idxa | ||
|
||
// insert service data key to the servicedata_keys table | ||
createdServiceDataKey, err := s.repository.CreateKey(ctx, key) | ||
if err != nil { | ||
return Key{}, err | ||
} | ||
|
||
// create relation | ||
_, err = s.relationService.Create(ctx, relation.RelationV2{ | ||
Object: relation.Object{ | ||
ID: resource.Idxa, | ||
NamespaceID: schema.ServiceDataKeyNamespace, | ||
}, | ||
Subject: relation.Subject{ | ||
ID: currentUser.ID, | ||
RoleID: schema.OwnerRole, | ||
Namespace: schema.UserPrincipal, | ||
}, | ||
}) | ||
if err != nil { | ||
return Key{}, err | ||
} | ||
|
||
return createdServiceDataKey, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package servicedata | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
type Repository interface { | ||
CreateKey(ctx context.Context, key Key) (Key, error) | ||
} | ||
|
||
type Key struct { | ||
ID string | ||
URN string | ||
ProjectID string | ||
Key string | ||
Description string | ||
ResourceID string | ||
} | ||
|
||
func (key Key) CreateURN() string { | ||
return fmt.Sprintf("%s:servicedata_key:%s", key.ProjectID, key.Key) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.