-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathseed_admin.go
More file actions
82 lines (71 loc) · 2.78 KB
/
Copy pathseed_admin.go
File metadata and controls
82 lines (71 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package cmd
import (
"context"
"flag"
"fmt"
"github.com/golang/glog"
"github.com/openshift-online/rh-trex-ai/pkg/api"
"github.com/openshift-online/rh-trex-ai/pkg/config"
"github.com/openshift-online/rh-trex-ai/pkg/db/db_session"
"github.com/spf13/cobra"
)
func NewSeedAdminCommand() *cobra.Command {
dbConfig := config.NewDatabaseConfig()
var username string
cmd := &cobra.Command{
Use: "seed-admin",
Short: "Create the initial platform:admin RoleBinding",
Long: "Seeds the first platform:admin user. This breaks the bootstrap chicken-and-egg: RBAC endpoints are themselves gated, so the first admin cannot grant themselves access through the API.",
Run: func(cmd *cobra.Command, args []string) {
if err := dbConfig.ReadFiles(); err != nil {
glog.Fatal(err)
}
connection := db_session.NewProdFactory(dbConfig)
db := connection.New(context.Background())
// Upsert user
userID := api.NewID()
result := db.Exec(
`INSERT INTO users (id, username, name, created_at, updated_at)
VALUES (?, ?, ?, NOW(), NOW())
ON CONFLICT (username) WHERE deleted_at IS NULL DO NOTHING`,
userID, username, username,
)
if result.Error != nil {
glog.Fatalf("Failed to upsert user: %v", result.Error)
}
// Resolve actual user ID (may already exist)
var resolvedUserID string
if err := db.Raw(`SELECT id FROM users WHERE username = ? AND deleted_at IS NULL`, username).Scan(&resolvedUserID).Error; err != nil {
glog.Fatalf("Failed to resolve user ID: %v", err)
}
// Look up platform:admin role
var roleID string
if err := db.Raw(`SELECT id FROM roles WHERE name = 'platform:admin' AND deleted_at IS NULL`).Scan(&roleID).Error; err != nil || roleID == "" {
glog.Fatal("platform:admin role not found — run migrations first")
}
// Create global binding (idempotent)
bindingResult := db.Exec(
`INSERT INTO role_bindings (id, role_id, scope, user_id, created_at, updated_at)
SELECT ?, ?, 'global', ?, NOW(), NOW()
WHERE NOT EXISTS (
SELECT 1 FROM role_bindings
WHERE role_id = ? AND scope = 'global' AND user_id = ? AND deleted_at IS NULL
)`,
api.NewID(), roleID, resolvedUserID, roleID, resolvedUserID,
)
if bindingResult.Error != nil {
glog.Fatalf("Failed to create admin binding: %v", bindingResult.Error)
}
if bindingResult.RowsAffected == 0 {
fmt.Printf("platform:admin binding already exists for user %q\n", username)
} else {
fmt.Printf("platform:admin binding created for user %q (id=%s)\n", username, resolvedUserID)
}
},
}
cmd.Flags().StringVar(&username, "username", "", "Username of the admin to seed (required)")
_ = cmd.MarkFlagRequired("username")
dbConfig.AddFlags(cmd.PersistentFlags())
cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
return cmd
}