Skip to content

Commit cc4e300

Browse files
authored
feat: manage platform users via GitOps (relation-level grants, bootstrap superuser SA, reconcile CLI) (#1719)
1 parent ed17ed9 commit cc4e300

34 files changed

Lines changed: 3858 additions & 1614 deletions

‎Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ TAG := $(shell git rev-list --tags --max-count=1)
44
VERSION := $(shell git describe --tags ${TAG})
55
.PHONY: build check fmt lint test test-race vet test-cover-html help install proto admin-app compose-up-dev
66
.DEFAULT_GOAL := build
7-
PROTON_COMMIT := "795c70f359264a3c21a4c6412097f139dfc387e6"
7+
PROTON_COMMIT := "ee05c27600bd7d2782da4fde0997f84aa69e7eeb"
88

99
admin-app:
1010
@echo " > generating admin build"

‎cmd/reconcile.go‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/MakeNowJust/heredoc"
8+
"github.com/raystack/frontier/internal/reconcile"
9+
cli "github.com/spf13/cobra"
10+
)
11+
12+
func ReconcileCommand(cliConfig *Config) *cli.Command {
13+
var (
14+
filePath string
15+
dryRun bool
16+
header string
17+
)
18+
cmd := &cli.Command{
19+
Use: "reconcile",
20+
Short: "Reconcile platform configuration to a desired-state file",
21+
Long: heredoc.Doc(`
22+
Make platform resources match a desired-state YAML file, through the admin API.
23+
24+
Supports the PlatformUser kind (platform admins and members) for now. The file
25+
decides who has access: anyone listed is added, anyone not listed is removed.
26+
Log in as a superuser (for example the bootstrap service account) with --header.
27+
`),
28+
Example: heredoc.Doc(`
29+
$ frontier reconcile -f platform-users.yaml --dry-run -H "Authorization:Basic <base64>"
30+
$ frontier reconcile -f platform-users.yaml -H "Authorization:Basic <base64>"
31+
`),
32+
Annotations: map[string]string{
33+
"group": "core",
34+
"client": "true",
35+
},
36+
RunE: func(cmd *cli.Command, args []string) error {
37+
data, err := os.ReadFile(filePath)
38+
if err != nil {
39+
return fmt.Errorf("read desired-state file: %w", err)
40+
}
41+
adminClient, err := createAdminClient(cliConfig.Host)
42+
if err != nil {
43+
return err
44+
}
45+
registry := map[string]reconcile.Reconciler{
46+
reconcile.KindPlatformUser: reconcile.NewPlatformUserReconciler(adminClient, header),
47+
}
48+
reports, runErr := reconcile.Run(cmd.Context(), registry, data, dryRun)
49+
for _, rep := range reports {
50+
printReconcileReport(cmd, rep)
51+
}
52+
return runErr
53+
},
54+
}
55+
cmd.Flags().StringVarP(&filePath, "file", "f", "", "Path to the desired-state YAML file")
56+
cmd.MarkFlagRequired("file")
57+
cmd.Flags().BoolVar(&dryRun, "dry-run", false, "Print the plan without applying changes")
58+
cmd.Flags().StringVarP(&header, "header", "H", "", "Header <key>:<value> for auth, e.g. 'Authorization:Basic <base64>'")
59+
bindFlagsFromClientConfig(cmd)
60+
return cmd
61+
}
62+
63+
func printReconcileReport(cmd *cli.Command, rep reconcile.Report) {
64+
if len(rep.Planned) == 0 {
65+
cmd.Printf("%s: no changes\n", rep.Kind)
66+
return
67+
}
68+
verb, count := "applied", rep.Applied
69+
if rep.DryRun {
70+
verb, count = "planned", len(rep.Planned)
71+
}
72+
cmd.Printf("%s (%s %d):\n", rep.Kind, verb, count)
73+
for _, p := range rep.Planned {
74+
cmd.Printf(" - %s\n", p)
75+
}
76+
}

‎cmd/root.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func New(cliConfig *Config) *cli.Command {
4949
cmd.AddCommand(PermissionCommand(cliConfig))
5050
cmd.AddCommand(PolicyCommand(cliConfig))
5151
cmd.AddCommand(SeedCommand(cliConfig))
52+
cmd.AddCommand(ReconcileCommand(cliConfig))
5253
cmd.AddCommand(configCommand())
5354
cmd.AddCommand(versionCommand())
5455
cmd.AddCommand(PreferencesCommand(cliConfig))

‎cmd/serve.go‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ func StartServer(logger *slog.Logger, cfg *config.Frontier) error {
212212
if err = deps.BootstrapService.MigrateRoles(ctx); err != nil {
213213
return err
214214
}
215-
// promote normal users to superusers
216-
if err = deps.BootstrapService.MakeSuperUsers(ctx); err != nil {
215+
// ensure the config-seeded bootstrap superuser service account (for automation/GitOps).
216+
// all other platform-user management is handled out-of-band via the GitOps reconcile flow.
217+
if err = deps.BootstrapService.EnsureBootstrapSuperUser(ctx); err != nil {
217218
return err
218219
}
219220

@@ -395,7 +396,7 @@ func buildAPIDependencies(
395396

396397
svUserRepo := postgres.NewServiceUserRepository(dbc)
397398
scUserCredRepo := postgres.NewServiceUserCredentialRepository(dbc)
398-
serviceUserService := serviceuser.NewService(logger, svUserRepo, scUserCredRepo, relationService)
399+
serviceUserService := serviceuser.NewService(logger, svUserRepo, scUserCredRepo, relationService, auditRecordRepository)
399400

400401
var mailDialer mailer.Dialer = mailer.NewMockDialer()
401402
if cfg.App.Mailer.SMTPHost != "" && cfg.App.Mailer.SMTPHost != "smtp.example.com" {
@@ -432,7 +433,7 @@ func buildAPIDependencies(
432433
// back here because role.Service depends on permission.Service
433434
permissionService.SetRoleService(roleService)
434435
policyService := policy.NewService(policyPGRepository, relationService, roleService)
435-
userService := user.NewService(userRepository, relationService, sessionService)
436+
userService := user.NewService(userRepository, relationService, sessionService, auditRecordRepository)
436437
patValidator := userpat.NewValidator(logger, userPATRepo, cfg.App.PAT)
437438
authnService := authenticate.NewService(logger, cfg.App.Authentication,
438439
postgres.NewFlowRepository(logger, dbc), mailDialer, tokenService, sessionService, userService, serviceUserService, webAuthConfig, patValidator)
@@ -569,14 +570,16 @@ func buildAPIDependencies(
569570
namespaceService,
570571
roleService,
571572
permissionService,
572-
userService,
573573
authzSchemaRepository,
574574
relationService,
575575
policyService,
576576
svUserRepo,
577577
cfg.App.PAT.DeniedPermissionsSet(),
578578
planService,
579579
planBlobRepository,
580+
svUserRepo,
581+
scUserCredRepo,
582+
serviceUserService,
580583
)
581584

582585
cascadeDeleter := deleter.NewCascadeDeleter(organizationService, projectService, resourceService,

‎config/sample.config.yaml‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,20 @@ app:
179179

180180
# platform level administration
181181
admin:
182-
# Email list of users which needs to be converted as superusers
183-
# if the user is already present in the system, it is promoted to su
184-
# if not, a new account is created with provided email id and promoted to su.
185-
# UUIDs/slugs of existing users can also be provided instead of email ids
186-
# but in that case a new user will not be created.
187-
users: []
182+
# bootstrap seeds a superuser SERVICE ACCOUNT from config (a username/password-style
183+
# client_id + client_secret), so automation like the GitOps reconcile flow always has
184+
# a superuser to log in as. This means you don't need an existing superuser to create
185+
# the first one. The account is created and promoted to superuser on every boot (safe
186+
# to run again), and the secret is rotated if you change it here. Log in with:
187+
# Authorization: Basic base64(client_id:client_secret).
188+
# Leave client_id/client_secret empty to turn this off — but then no superuser is
189+
# seeded, and since granting one needs a superuser, none can be created via the API
190+
# either. client_id must be a UUID (it is the service-account credential id); generate
191+
# one (e.g. uuidgen) and keep it stable. client_secret is your chosen password.
192+
bootstrap:
193+
client_id: ""
194+
client_secret: ""
195+
# title: "GitOps Bootstrap Superuser"
188196
# smtp configuration for sending emails
189197
mailer:
190198
smtp_host: smtp.example.com

‎core/serviceuser/errors.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ var (
1212
ErrConflict = errors.New("service user already exist")
1313
ErrEmptyKey = errors.New("empty key")
1414
ErrDisabled = errors.New("service user is disabled")
15+
// ErrProtected marks a server-managed service user (the config-bootstrapped
16+
// bootstrap SA) whose credentials must not be mutated via the API.
17+
ErrProtected = errors.New("service user is protected")
1518
)

‎core/serviceuser/mocks/audit_record_repository.go‎

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)