mirror of
https://github.com/GlueOps/autoglue.git
synced 2026-02-13 04:40:05 +01:00
fix: api keys form bugfix and org key sweeper job
Signed-off-by: allanice001 <allanice001@gmail.com>
This commit is contained in:
15
cmd/serve.go
15
cmd/serve.go
@@ -156,6 +156,21 @@ var serveCmd = &cobra.Command{
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to enqueue cluster bootstrap: %v", err)
|
log.Printf("failed to enqueue cluster bootstrap: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, err = jobs.Enqueue(
|
||||||
|
context.Background(),
|
||||||
|
uuid.NewString(),
|
||||||
|
"org_key_sweeper",
|
||||||
|
bg.OrgKeySweeperArgs{
|
||||||
|
IntervalS: 3600,
|
||||||
|
RetentionDays: 10,
|
||||||
|
},
|
||||||
|
archer.WithMaxRetries(1),
|
||||||
|
archer.WithScheduleTime(time.Now()),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to enqueue org_key_sweeper: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = auth.Refresh(rt.DB, rt.Cfg.JWTPrivateEncKey)
|
_ = auth.Refresh(rt.DB, rt.Cfg.JWTPrivateEncKey)
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -104,6 +104,8 @@ components:
|
|||||||
$ref: '#/components/schemas/dto.LoadBalancerResponse'
|
$ref: '#/components/schemas/dto.LoadBalancerResponse'
|
||||||
id:
|
id:
|
||||||
type: string
|
type: string
|
||||||
|
kubeconfig:
|
||||||
|
type: string
|
||||||
last_error:
|
last_error:
|
||||||
type: string
|
type: string
|
||||||
name:
|
name:
|
||||||
@@ -113,6 +115,10 @@ components:
|
|||||||
$ref: '#/components/schemas/dto.NodePoolResponse'
|
$ref: '#/components/schemas/dto.NodePoolResponse'
|
||||||
type: array
|
type: array
|
||||||
uniqueItems: false
|
uniqueItems: false
|
||||||
|
org_key:
|
||||||
|
type: string
|
||||||
|
org_secret:
|
||||||
|
type: string
|
||||||
random_token:
|
random_token:
|
||||||
type: string
|
type: string
|
||||||
region:
|
region:
|
||||||
@@ -1037,6 +1043,8 @@ components:
|
|||||||
type: object
|
type: object
|
||||||
models.APIKey:
|
models.APIKey:
|
||||||
properties:
|
properties:
|
||||||
|
cluster_id:
|
||||||
|
type: string
|
||||||
created_at:
|
created_at:
|
||||||
format: date-time
|
format: date-time
|
||||||
type: string
|
type: string
|
||||||
@@ -1046,6 +1054,8 @@ components:
|
|||||||
id:
|
id:
|
||||||
format: uuid
|
format: uuid
|
||||||
type: string
|
type: string
|
||||||
|
is_ephemeral:
|
||||||
|
type: boolean
|
||||||
last_used_at:
|
last_used_at:
|
||||||
format: date-time
|
format: date-time
|
||||||
type: string
|
type: string
|
||||||
@@ -1056,6 +1066,8 @@ components:
|
|||||||
type: string
|
type: string
|
||||||
prefix:
|
prefix:
|
||||||
type: string
|
type: string
|
||||||
|
purpose:
|
||||||
|
type: string
|
||||||
revoked:
|
revoked:
|
||||||
type: boolean
|
type: boolean
|
||||||
scope:
|
scope:
|
||||||
|
|||||||
@@ -129,6 +129,12 @@ func NewJobs(gdb *gorm.DB, dbUrl string) (*Jobs, error) {
|
|||||||
archer.WithTimeout(60*time.Minute),
|
archer.WithTimeout(60*time.Minute),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
c.Register(
|
||||||
|
"org_key_sweeper",
|
||||||
|
OrgKeySweeperWorker(gdb, jobs),
|
||||||
|
archer.WithInstances(1),
|
||||||
|
archer.WithTimeout(5*time.Minute),
|
||||||
|
)
|
||||||
return jobs, nil
|
return jobs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
95
internal/bg/org_key_sweeper.go
Normal file
95
internal/bg/org_key_sweeper.go
Normal file
@@ -0,0 +1,95 @@
|
|||||||
|
package bg
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/dyaksa/archer"
|
||||||
|
"github.com/dyaksa/archer/job"
|
||||||
|
"github.com/glueops/autoglue/internal/models"
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OrgKeySweeperArgs struct {
|
||||||
|
IntervalS int `json:"interval_seconds,omitempty"`
|
||||||
|
RetentionDays int `json:"retention_days,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type OrgKeySweeperResult struct {
|
||||||
|
Status string `json:"status"`
|
||||||
|
MarkedRevoked int `json:"marked_revoked"`
|
||||||
|
DeletedEphemeral int `json:"deleted_ephemeral"`
|
||||||
|
ElapsedMs int `json:"elapsed_ms"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func OrgKeySweeperWorker(db *gorm.DB, jobs *Jobs) archer.WorkerFn {
|
||||||
|
return func(ctx context.Context, j job.Job) (any, error) {
|
||||||
|
args := OrgKeySweeperArgs{
|
||||||
|
IntervalS: 3600,
|
||||||
|
RetentionDays: 10,
|
||||||
|
}
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
_ = j.ParseArguments(&args)
|
||||||
|
if args.IntervalS <= 0 {
|
||||||
|
args.IntervalS = 3600
|
||||||
|
}
|
||||||
|
if args.RetentionDays <= 0 {
|
||||||
|
args.RetentionDays = 10
|
||||||
|
}
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
// 1) Mark expired keys as revoked
|
||||||
|
res1 := db.Model(&models.APIKey{}).
|
||||||
|
Where("expires_at IS NOT NULL AND expires_at <= ? AND revoked = false", now).
|
||||||
|
Updates(map[string]any{
|
||||||
|
"revoked": true,
|
||||||
|
"updated_at": now,
|
||||||
|
})
|
||||||
|
|
||||||
|
if res1.Error != nil {
|
||||||
|
log.Error().Err(res1.Error).Msg("[org_key_sweeper] mark expired revoked failed")
|
||||||
|
return nil, res1.Error
|
||||||
|
}
|
||||||
|
markedRevoked := int(res1.RowsAffected)
|
||||||
|
|
||||||
|
// 2) Hard-delete ephemeral keys that are revoked and older than retention
|
||||||
|
cutoff := now.Add(-time.Duration(args.RetentionDays) * 24 * time.Hour)
|
||||||
|
res2 := db.
|
||||||
|
Where("is_ephemeral = ? AND revoked = ? AND updated_at <= ?", true, true, cutoff).
|
||||||
|
Delete(&models.APIKey{})
|
||||||
|
|
||||||
|
if res2.Error != nil {
|
||||||
|
log.Error().Err(res2.Error).Msg("[org_key_sweeper] delete revoked ephemeral keys failed")
|
||||||
|
return nil, res2.Error
|
||||||
|
}
|
||||||
|
deletedEphemeral := int(res2.RowsAffected)
|
||||||
|
|
||||||
|
out := OrgKeySweeperResult{
|
||||||
|
Status: "ok",
|
||||||
|
MarkedRevoked: markedRevoked,
|
||||||
|
DeletedEphemeral: deletedEphemeral,
|
||||||
|
ElapsedMs: int(time.Since(start).Milliseconds()),
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Info().
|
||||||
|
Int("marked_revoked", markedRevoked).
|
||||||
|
Int("deleted_ephemeral", deletedEphemeral).
|
||||||
|
Msg("[org_key_sweeper] cleanup tick ok")
|
||||||
|
|
||||||
|
// Re-enqueue the sweeper
|
||||||
|
next := time.Now().Add(time.Duration(args.IntervalS) * time.Second)
|
||||||
|
_, _ = jobs.Enqueue(
|
||||||
|
ctx,
|
||||||
|
uuid.NewString(),
|
||||||
|
"org_key_sweeper",
|
||||||
|
args,
|
||||||
|
archer.WithScheduleTime(next),
|
||||||
|
archer.WithMaxRetries(1),
|
||||||
|
)
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -158,10 +158,11 @@ func ClusterPrepareWorker(db *gorm.DB, jobs *Jobs) archer.WorkerFn {
|
|||||||
dtoCluster.Kubeconfig = &kubeconfig
|
dtoCluster.Kubeconfig = &kubeconfig
|
||||||
}
|
}
|
||||||
|
|
||||||
orgKey, orgSecret, err := createOrgScopedKeyForPayload(
|
orgKey, orgSecret, err := findOrCreateClusterAutomationKey(
|
||||||
db,
|
db,
|
||||||
c.OrganizationID,
|
c.OrganizationID,
|
||||||
fmt.Sprintf("cluster-%s-%s", c.Name, c.ID.String()),
|
c.ID,
|
||||||
|
24*time.Hour,
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -584,12 +585,28 @@ func randomB64URL(n int) (string, error) {
|
|||||||
return base64.RawURLEncoding.EncodeToString(b), nil
|
return base64.RawURLEncoding.EncodeToString(b), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func createOrgScopedKeyForPayload(db *gorm.DB, orgID uuid.UUID, name string) (orgKey string, orgSecret string, err error) {
|
func findOrCreateClusterAutomationKey(
|
||||||
|
db *gorm.DB,
|
||||||
|
orgID uuid.UUID,
|
||||||
|
clusterID uuid.UUID,
|
||||||
|
ttl time.Duration,
|
||||||
|
) (orgKey string, orgSecret string, err error) {
|
||||||
|
now := time.Now()
|
||||||
|
name := fmt.Sprintf("cluster-%s-bastion", clusterID.String())
|
||||||
|
|
||||||
|
// 1) Delete any existing ephemeral cluster-bastion key for this org+cluster
|
||||||
|
if err := db.Where(
|
||||||
|
"org_id = ? AND scope = ? AND purpose = ? AND cluster_id = ? AND is_ephemeral = ?",
|
||||||
|
orgID, "org", "cluster_bastion", clusterID, true,
|
||||||
|
).Delete(&models.APIKey{}).Error; err != nil {
|
||||||
|
return "", "", fmt.Errorf("delete existing cluster key: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2) Mint a fresh keypair
|
||||||
keySuffix, err := randomB64URL(16)
|
keySuffix, err := randomB64URL(16)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", fmt.Errorf("entropy_error: %w", err)
|
return "", "", fmt.Errorf("entropy_error: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sec, err := randomB64URL(32)
|
sec, err := randomB64URL(32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", fmt.Errorf("entropy_error: %w", err)
|
return "", "", fmt.Errorf("entropy_error: %w", err)
|
||||||
@@ -604,13 +621,25 @@ func createOrgScopedKeyForPayload(db *gorm.DB, orgID uuid.UUID, name string) (or
|
|||||||
return "", "", fmt.Errorf("hash_error: %w", err)
|
return "", "", fmt.Errorf("hash_error: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exp := now.Add(ttl)
|
||||||
|
|
||||||
|
prefix := orgKey
|
||||||
|
if len(prefix) > 12 {
|
||||||
|
prefix = prefix[:12]
|
||||||
|
}
|
||||||
|
|
||||||
rec := models.APIKey{
|
rec := models.APIKey{
|
||||||
OrgID: &orgID,
|
OrgID: &orgID,
|
||||||
Scope: "org",
|
Scope: "org",
|
||||||
|
Purpose: "cluster_bastion",
|
||||||
|
ClusterID: &clusterID,
|
||||||
|
IsEphemeral: true,
|
||||||
Name: name,
|
Name: name,
|
||||||
KeyHash: keyHash,
|
KeyHash: keyHash,
|
||||||
SecretHash: &secretHash,
|
SecretHash: &secretHash,
|
||||||
ExpiresAt: nil,
|
ExpiresAt: &exp,
|
||||||
|
Revoked: false,
|
||||||
|
Prefix: &prefix,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.Create(&rec).Error; err != nil {
|
if err := db.Create(&rec).Error; err != nil {
|
||||||
|
|||||||
@@ -585,13 +585,22 @@ func CreateOrgKey(db *gorm.DB) http.HandlerFunc {
|
|||||||
exp = &e
|
exp = &e
|
||||||
}
|
}
|
||||||
|
|
||||||
|
prefix := orgKey
|
||||||
|
if len(prefix) > 12 {
|
||||||
|
prefix = prefix[:12]
|
||||||
|
}
|
||||||
|
|
||||||
rec := models.APIKey{
|
rec := models.APIKey{
|
||||||
OrgID: &oid,
|
OrgID: &oid,
|
||||||
Scope: "org",
|
Scope: "org",
|
||||||
|
Purpose: "user",
|
||||||
|
IsEphemeral: false,
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
KeyHash: keyHash,
|
KeyHash: keyHash,
|
||||||
SecretHash: &secretHash,
|
SecretHash: &secretHash,
|
||||||
ExpiresAt: exp,
|
ExpiresAt: exp,
|
||||||
|
Revoked: false,
|
||||||
|
Prefix: &prefix,
|
||||||
}
|
}
|
||||||
if err := db.Create(&rec).Error; err != nil {
|
if err := db.Create(&rec).Error; err != nil {
|
||||||
utils.WriteError(w, 500, "db_error", err.Error())
|
utils.WriteError(w, 500, "db_error", err.Error())
|
||||||
|
|||||||
@@ -8,12 +8,15 @@ import (
|
|||||||
|
|
||||||
type APIKey struct {
|
type APIKey struct {
|
||||||
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:gen_random_uuid()" json:"id" format:"uuid"`
|
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:gen_random_uuid()" json:"id" format:"uuid"`
|
||||||
|
OrgID *uuid.UUID `json:"org_id,omitempty" format:"uuid"`
|
||||||
|
Scope string `gorm:"not null;default:''" json:"scope"`
|
||||||
|
Purpose string `json:"purpose"`
|
||||||
|
ClusterID *uuid.UUID `json:"cluster_id,omitempty"`
|
||||||
|
IsEphemeral bool `json:"is_ephemeral"`
|
||||||
Name string `gorm:"not null;default:''" json:"name"`
|
Name string `gorm:"not null;default:''" json:"name"`
|
||||||
KeyHash string `gorm:"uniqueIndex;not null" json:"-"`
|
KeyHash string `gorm:"uniqueIndex;not null" json:"-"`
|
||||||
Scope string `gorm:"not null;default:''" json:"scope"`
|
|
||||||
UserID *uuid.UUID `json:"user_id,omitempty" format:"uuid"`
|
|
||||||
OrgID *uuid.UUID `json:"org_id,omitempty" format:"uuid"`
|
|
||||||
SecretHash *string `json:"-"`
|
SecretHash *string `json:"-"`
|
||||||
|
UserID *uuid.UUID `json:"user_id,omitempty" format:"uuid"`
|
||||||
ExpiresAt *time.Time `json:"expires_at,omitempty" format:"date-time"`
|
ExpiresAt *time.Time `json:"expires_at,omitempty" format:"date-time"`
|
||||||
Revoked bool `gorm:"not null;default:false" json:"revoked"`
|
Revoked bool `gorm:"not null;default:false" json:"revoked"`
|
||||||
Prefix *string `json:"prefix,omitempty"`
|
Prefix *string `json:"prefix,omitempty"`
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
;
|
;
|
||||||
|
|
||||||
// src/pages/ClustersPage.tsx
|
// src/pages/ClustersPage.tsx
|
||||||
|
|
||||||
import { useEffect, useMemo, useState } from "react";
|
import { useEffect, useMemo, useState } from "react";
|
||||||
@@ -28,36 +27,6 @@ import { Label } from "@/components/ui/label.tsx";
|
|||||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select.tsx";
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select.tsx";
|
||||||
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table.tsx";
|
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table.tsx";
|
||||||
import { Textarea } from "@/components/ui/textarea.tsx";
|
import { Textarea } from "@/components/ui/textarea.tsx";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -35,10 +35,12 @@ import {
|
|||||||
TableRow,
|
TableRow,
|
||||||
} from "@/components/ui/table.tsx"
|
} from "@/components/ui/table.tsx"
|
||||||
|
|
||||||
|
// 1) No coerce; we’ll do the conversion in onChange
|
||||||
const createSchema = z.object({
|
const createSchema = z.object({
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
expires_in_hours: z.number().min(1).max(43800),
|
expires_in_hours: z.number().int().min(1).max(43800),
|
||||||
})
|
})
|
||||||
|
|
||||||
type CreateValues = z.infer<typeof createSchema>
|
type CreateValues = z.infer<typeof createSchema>
|
||||||
|
|
||||||
export const OrgApiKeys = () => {
|
export const OrgApiKeys = () => {
|
||||||
@@ -52,6 +54,7 @@ export const OrgApiKeys = () => {
|
|||||||
queryFn: () => withRefresh(() => api.listOrgKeys({ id: orgId! })),
|
queryFn: () => withRefresh(() => api.listOrgKeys({ id: orgId! })),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 2) Form holds numbers directly
|
||||||
const form = useForm<CreateValues>({
|
const form = useForm<CreateValues>({
|
||||||
resolver: zodResolver(createSchema),
|
resolver: zodResolver(createSchema),
|
||||||
defaultValues: {
|
defaultValues: {
|
||||||
@@ -71,7 +74,7 @@ export const OrgApiKeys = () => {
|
|||||||
void qc.invalidateQueries({ queryKey: ["org:keys", orgId] })
|
void qc.invalidateQueries({ queryKey: ["org:keys", orgId] })
|
||||||
setShowSecret({ key: resp.org_key, secret: resp.org_secret })
|
setShowSecret({ key: resp.org_key, secret: resp.org_secret })
|
||||||
toast.success("Key created")
|
toast.success("Key created")
|
||||||
form.reset({ name: "", expires_in_hours: undefined })
|
form.reset({ name: "", expires_in_hours: 720 })
|
||||||
},
|
},
|
||||||
onError: (e: any) => toast.error(e?.message ?? "Failed to create key"),
|
onError: (e: any) => toast.error(e?.message ?? "Failed to create key"),
|
||||||
})
|
})
|
||||||
@@ -124,7 +127,17 @@ export const OrgApiKeys = () => {
|
|||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>Expires In (hours)</FormLabel>
|
<FormLabel>Expires In (hours)</FormLabel>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input placeholder="e.g. 720" {...field} />
|
<Input
|
||||||
|
type="number"
|
||||||
|
placeholder="e.g. 720"
|
||||||
|
{...field}
|
||||||
|
// 3) Convert string → number (or undefined if empty)
|
||||||
|
value={field.value ?? ""}
|
||||||
|
onChange={(e) => {
|
||||||
|
const v = e.target.value
|
||||||
|
field.onChange(v === "" ? undefined : Number(v))
|
||||||
|
}}
|
||||||
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
|
|||||||
Reference in New Issue
Block a user