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

ReleaseTrain Role Based Access Control #851

Merged
merged 2 commits into from
Aug 4, 2023
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
15 changes: 11 additions & 4 deletions services/cd-service/pkg/repository/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,7 @@ func (c *DeployApplicationVersion) Transform(ctx context.Context, state *State)
}

type ReleaseTrain struct {
Authentication
Target string
Team string
}
Expand Down Expand Up @@ -1055,6 +1056,11 @@ func (c *ReleaseTrain) Transform(ctx context.Context, state *State) (string, err
if envConfig.Upstream == nil {
return fmt.Sprintf("Environment %q does not have upstream configured - exiting.", envName), nil
}
err := state.checkUserPermissions(ctx, envName, "*", auth.PermissionDeployReleaseTrain, c.RBACConfig)
if err != nil {
return "", err
}

sven-urbanski-freiheit-com marked this conversation as resolved.
Show resolved Hide resolved
var upstreamLatest = envConfig.Upstream.Latest
var upstreamEnvName = envConfig.Upstream.Environment

Expand Down Expand Up @@ -1136,10 +1142,11 @@ func (c *ReleaseTrain) Transform(ctx context.Context, state *State) (string, err
}

d := &DeployApplicationVersion{
Environment: envName, // here we deploy to the next env
Application: appName,
Version: versionToDeploy,
LockBehaviour: api.LockBehavior_Record,
Environment: envName, // here we deploy to the next env
Application: appName,
Version: versionToDeploy,
LockBehaviour: api.LockBehavior_Record,
Authentication: c.Authentication,
}
transform, err := d.Transform(ctx, state)
if err != nil {
Expand Down
78 changes: 78 additions & 0 deletions services/cd-service/pkg/repository/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,34 @@ func TestRbacTransformerTest(t *testing.T) {
Transformers []Transformer
ExpectedError string
}{
{
Name: "able to create release train with permissions policy",
Transformers: ReleaseTrainTestSetup(&ReleaseTrain{
Target: envProduction,
Authentication: Authentication{RBACConfig: auth.RBACConfig{DexEnabled: true, Policy: map[string]*auth.Permission{
"developer,DeployReleaseTrain,production:production,*,allow": {Role: "developer"},
"developer,DeployRelease,production:*,test,allow": {Role: "developer"},
}}},
}),
},
{
Name: "unable to create release train without permissions policy: Missing DeployRelease permission",
Transformers: ReleaseTrainTestSetup(&ReleaseTrain{
Target: envProduction,
Authentication: Authentication{RBACConfig: auth.RBACConfig{DexEnabled: true, Policy: map[string]*auth.Permission{
"developer,DeployReleaseTrain,production:production,*,allow": {Role: "developer"},
}}},
}),
ExpectedError: "rpc error: code = Internal desc = internal error",
},
{
Name: "unable to create release train without permissions policy: Missing ReleaseTrain permission",
Transformers: ReleaseTrainTestSetup(&ReleaseTrain{
Target: envProduction,
Authentication: Authentication{RBACConfig: auth.RBACConfig{DexEnabled: true, Policy: map[string]*auth.Permission{}}},
}),
ExpectedError: "user does not have permissions for: developer,DeployReleaseTrain,production:production,*,allow",
},
{
Name: "able to create application version with permissions policy",
Transformers: []Transformer{
Expand Down Expand Up @@ -1157,6 +1185,56 @@ func TestRbacTransformerTest(t *testing.T) {
}
}

// Helper method to setup release train unit tests.
func ReleaseTrainTestSetup(releaseTrainTransformer Transformer) []Transformer {
return append([]Transformer{
&CreateEnvironment{
Environment: envProduction,
Config: config.EnvironmentConfig{
Upstream: &config.EnvironmentConfigUpstream{
Environment: envAcceptance, // train drives from acceptance to production
},
},
},
&CreateEnvironment{
Environment: envAcceptance,
Config: config.EnvironmentConfig{
Upstream: &config.EnvironmentConfigUpstream{
Latest: true,
},
},
},
&CreateApplicationVersion{
Application: "test",
Manifests: map[string]string{
envProduction: "productionmanifest",
envAcceptance: "acceptancenmanifest",
},
},
&DeployApplicationVersion{
Environment: envProduction,
Application: "test",
Version: 1,
},
&CreateApplicationVersion{
Application: "test",
Manifests: map[string]string{
envProduction: "productionmanifest",
envAcceptance: "acceptancenmanifest",
},
},
&DeployApplicationVersion{
Environment: envAcceptance,
Application: "test",
Version: 1,
},
&DeployApplicationVersion{
Environment: envAcceptance,
Application: "test",
Version: 2,
}}, releaseTrainTransformer)
}

func TestTransformer(t *testing.T) {
c1 := config.EnvironmentConfig{Upstream: &config.EnvironmentConfigUpstream{Latest: true}}

Expand Down
5 changes: 3 additions & 2 deletions services/cd-service/pkg/service/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ func (d *BatchServer) processAction(
return nil, nil, status.Error(codes.InvalidArgument, "invalid Team name")
}
return &repository.ReleaseTrain{
Target: in.Target,
Team: in.Team,
Target: in.Target,
Team: in.Team,
Authentication: repository.Authentication{RBACConfig: d.RBACConfig},
}, &api.BatchResult{
Result: &api.BatchResult_ReleaseTrain{
ReleaseTrain: &api.ReleaseTrainResponse{Target: in.Target, Team: in.Team},
Expand Down