Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat(action): support access-token auth for workload identity (#19416)

rebeliceaef4ee9ab72f
diff --git a/action/command/api.go b/action/command/api.go
index bd83d6f7ea..1fbbed7370 100644
--- a/action/command/api.go
+++ b/action/command/api.go
@@ -73,13 +73,22 @@ type Client struct {
options ClientOptions
}
-// NewClient returns the new Bytebase API client with default options.
+// NewClient returns the new Bytebase API client with default options using service account auth.
func NewClient(url, serviceAccount, serviceAccountSecret string) (*Client, error) {
return NewClientWithOptions(url, serviceAccount, serviceAccountSecret, DefaultClientOptions())
}
-// NewClientWithOptions returns a new Bytebase API client with custom options.
+// NewClientWithAccessToken returns a new Bytebase API client using a pre-obtained access token.
+func NewClientWithAccessToken(url, accessToken string) (*Client, error) {
+ return createClient(url, accessToken, "", "", DefaultClientOptions())
+}
+
+// NewClientWithOptions returns a new Bytebase API client with custom options using service account auth.
func NewClientWithOptions(url, serviceAccount, serviceAccountSecret string, opts ClientOptions) (*Client, error) {
+ return createClient(url, "", serviceAccount, serviceAccountSecret, opts)
+}
+
+func createClient(url, accessToken, serviceAccount, serviceAccountSecret string, opts ClientOptions) (*Client, error) {
if opts.PageSize <= 0 {
opts.PageSize = 100
}
@@ -97,8 +106,15 @@ func NewClientWithOptions(url, serviceAccount, serviceAccountSecret string, opts
httpClient = &http.Client{Timeout: timeout}
}
- // Create token refresher function
- tokenRefresher := getTokenRefresher(httpClient, serviceAccount, serviceAccountSecret, url)
+ var tokenRefresher func(ctx context.Context) (string, error)
+ if accessToken != "" {
+ // Use the provided access token directly; no refresh needed.
+ tokenRefresher = func(_ context.Context) (string, error) {
+ return accessToken, nil
+ }
+ } else {
+ tokenRefresher = getTokenRefresher(httpClient, serviceAccount, serviceAccountSecret, url)
+ }
// Create unified interceptor
unifiedInt := newUnifiedInterceptor(opts.RetryConfig, tokenRefresher)
diff --git a/action/command/rollout.go b/action/command/rollout.go
index 3e1787cab3..fbea41d60a 100644
--- a/action/command/rollout.go
+++ b/action/command/rollout.go
@@ -55,7 +55,7 @@ func runRollout(w *world.World) func(command *cobra.Command, _ []string) error {
}()
w.IsRollout = true
ctx := command.Context()
- client, err := NewClient(w.URL, w.ServiceAccount, w.ServiceAccountSecret)
+ client, err := NewClientFromWorld(w)
if err != nil {
return errors.Wrapf(err, "failed to create client")
}
diff --git a/action/command/root.go b/action/command/root.go
index acbb94bf6b..56ac79c28f 100644
--- a/action/command/root.go
+++ b/action/command/root.go
@@ -25,6 +25,7 @@ func NewRootCommand(w *world.World) *cobra.Command {
cmd.PersistentFlags().StringVar(&w.URL, "url", "https://demo.bytebase.com", "Bytebase URL")
cmd.PersistentFlags().StringVar(&w.ServiceAccount, "service-account", "", "Bytebase Service account")
cmd.PersistentFlags().StringVar(&w.ServiceAccountSecret, "service-account-secret", "", "Bytebase Service account secret")
+ cmd.PersistentFlags().StringVar(&w.AccessToken, "access-token", "", "Bytebase access token (alternative to service account auth, e.g. from workload identity exchange)")
cmd.PersistentFlags().StringVar(&w.Project, "project", "projects/hr", "Bytebase project")
cmd.PersistentFlags().StringSliceVar(&w.Targets, "targets", []string{"instances/test-sample-instance/databases/hr_test", "instances/prod-sample-instance/databases/hr_prod"}, "Bytebase targets. Either one or more databases or a single databaseGroup")
cmd.PersistentFlags().StringVar(&w.FilePattern, "file-pattern", "", "File pattern to glob migration files")
@@ -53,6 +54,14 @@ func rootPreRun(w *world.World) func(cmd *cobra.Command, args []string) error {
}
}
+// NewClientFromWorld creates a Client using the authentication method configured in World.
+func NewClientFromWorld(w *world.World) (*Client, error) {
+ if w.AccessToken != "" {
+ return NewClientWithAccessToken(w.URL, w.AccessToken)
+ }
+ return NewClient(w.URL, w.ServiceAccount, w.ServiceAccountSecret)
+}
+
func CheckVersionCompatibility(w *world.World, client *Client, cliVersion string) {
if cliVersion == "unknown" {
w.Logger.Warn("CLI version unknown, unable to check compatibility")
diff --git a/action/world/world.go b/action/world/world.go
index 20ba507045..c5ca9a97c4 100644
--- a/action/world/world.go
+++ b/action/world/world.go
@@ -21,6 +21,7 @@ type World struct {
URL string
ServiceAccount string
ServiceAccountSecret string
+ AccessToken string // Alternative to service account auth, e.g. from workload identity exchange
Project string // projects/{project}
Targets []string
FilePattern string