Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: log schema migration #98

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func StartServer(logger *log.Zap, cfg *config.Shield) error {
}

schemaMigrationService := schema.NewSchemaMigrationService(
logger,
schema.AppConfig{ConfigStorage: parsedResourcesConfigURL.Scheme},
schemaConfigRepository,
resourcePGRepository,
Expand Down
15 changes: 13 additions & 2 deletions internal/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/goto/shield/core/role"
"github.com/goto/shield/core/user"

"github.com/goto/salt/log"
"golang.org/x/exp/maps"
)

Expand Down Expand Up @@ -105,6 +106,7 @@ type SchemaMigrationConfig struct {
}

type SchemaService struct {
logger log.Logger
appConfig AppConfig
schemaConfig FileService
pgRepository PGRepository
Expand All @@ -118,6 +120,7 @@ type SchemaService struct {
}

func NewSchemaMigrationService(
logger log.Logger,
appConfig AppConfig,
schemaConfig FileService,
pgRepository PGRepository,
Expand All @@ -130,6 +133,7 @@ func NewSchemaMigrationService(
schemaMigrationConfig SchemaMigrationConfig,
) *SchemaService {
return &SchemaService{
logger: logger,
appConfig: appConfig,
schemaConfig: schemaConfig,
pgRepository: pgRepository,
Expand Down Expand Up @@ -198,6 +202,7 @@ func (s SchemaService) RunMigrations(ctx context.Context) error {
backend = st[0]
resourceType = st[1]
}
s.logger.Info(fmt.Sprintf("create namespace %s", namespaceId))
_, err := s.namespaceService.Upsert(ctx, namespace.Namespace{
ID: namespaceId,
Name: namespaceId,
Expand All @@ -210,6 +215,7 @@ func (s SchemaService) RunMigrations(ctx context.Context) error {

// create roles
for roleId, principals := range v.Roles {
s.logger.Info(fmt.Sprintf("create role %s with principals %s under namespace %s", roleId, principals, namespaceId))
_, err := s.roleService.Upsert(ctx, role.Role{
ID: fmt.Sprintf("%s:%s", namespaceId, roleId),
Name: roleId,
Expand All @@ -223,6 +229,7 @@ func (s SchemaService) RunMigrations(ctx context.Context) error {

// create role for inherited namespaces
for _, ins := range v.InheritedNamespaces {
s.logger.Info(fmt.Sprintf("create role %s from inherited namespace %s under namespace %s", ins.Name, ins.NamespaceId, namespaceId))
_, err := s.roleService.Upsert(ctx, role.Role{
ID: fmt.Sprintf("%s:%s", namespaceId, ins.Name),
Name: ins.Name,
Expand All @@ -237,6 +244,7 @@ func (s SchemaService) RunMigrations(ctx context.Context) error {
// create actions
// IMP: we should depreciate actions with principals
for actionId := range v.Permissions {
s.logger.Info(fmt.Sprintf("create action %s under namespace %s", actionId, namespaceId))
_, err := s.actionService.Upsert(ctx, action.Action{
ID: fmt.Sprintf("%s.%s", actionId, namespaceId),
Name: actionId,
Expand All @@ -261,10 +269,13 @@ func (s SchemaService) RunMigrations(ctx context.Context) error {
return fmt.Errorf("%w: role %s not associated with namespace: %s", ErrInvalidDetail, transformedRole.ID, transformedRole.NamespaceID)
}

roleId := GetRoleID(GetNamespace(transformedRole.NamespaceID), transformedRole.ID)
actionId := fmt.Sprintf("%s.%s", actionId, namespaceId)
s.logger.Info(fmt.Sprintf("create policy for role %s on namespace %s with action %s", roleId, namespaceId, actionId))
_, err = s.policyService.Upsert(ctx, &policy.Policy{
RoleID: GetRoleID(GetNamespace(transformedRole.NamespaceID), transformedRole.ID),
RoleID: roleId,
NamespaceID: namespaceId,
ActionID: fmt.Sprintf("%s.%s", actionId, namespaceId),
ActionID: actionId,
})
if err != nil {
return fmt.Errorf("%w: %s", ErrMigration, err.Error())
Expand Down
7 changes: 7 additions & 0 deletions internal/schema/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ func TestAppendIfUnique(t *testing.T) {
fmt.Println(AppendIfUnique([]string{"1", "2", "3"}, []string{"3", "4"}))
assert.ElementsMatch(t, AppendIfUnique([]string{"1", "2", "3"}, []string{"3", "4"}), []string{"1", "2", "3", "4"})
}

func TestGetNamespace(t *testing.T) {
fmt.Println(GetNamespace("project"))
assert.Equal(t, "shield/project", GetNamespace("project"))
fmt.Println(GetNamespace("entropy/firehose"))
assert.Equal(t, "entropy/firehose", GetNamespace("entropy/firehose"))
}
Loading