Skip to content

Commit 4c5a483

Browse files
committed
feat(auth): config oauth2 default login during build time
1 parent dcc639e commit 4c5a483

File tree

10 files changed

+65
-33
lines changed

10 files changed

+65
-33
lines changed

.github/workflows/releases.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,9 @@ jobs:
2626
args: release --clean
2727
env:
2828
GITHUB_TOKEN: ${{ secrets.botGitHubToken }}
29+
INSTILL_API_HOSTNAME: "api.instill.tech"
30+
INSTILL_OAUTH_HOSTNAME: "auth.instill.tech"
31+
INSTILL_OAUTH_AUDIENCE: "https://api.instill.tech"
32+
INSTILL_OAUTH_ISSUER: "https://auth.instill.tech/"
2933
INSTILL_OAUTH_CLIENT_ID: ${{ secrets.oauth2ClientId }}
3034
INSTILL_OAUTH_CLIENT_SECRET: ${{ secrets.oauth2ClientSecret }}

.goreleaser.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ builds:
2121
- -s -w
2222
- -X github.com/instill-ai/cli/internal/build.Version={{ .Version }}
2323
- -X github.com/instill-ai/cli/internal/build.Date={{ time "2006-01-02" }}
24+
- -X github.com/instill-ai/cli/internal/oauth2.apiHostname={{ .Env.INSTILL_API_HOSTNAME }}
25+
- -X github.com/instill-ai/cli/internal/oauth2.oauth2Hostname={{ .Env.INSTILL_OAUTH_HOSTNAME }}
26+
- -X github.com/instill-ai/cli/internal/oauth2.oauth2Audience={{ .Env.INSTILL_OAUTH_AUDIENCE }}
27+
- -X github.com/instill-ai/cli/internal/oauth2.oauth2Issuer={{ .Env.INSTILL_OAUTH_ISSUER }}
2428
- -X github.com/instill-ai/cli/internal/oauth2.clientID={{ .Env.INSTILL_OAUTH_CLIENT_ID }}
2529
- -X github.com/instill-ai/cli/internal/oauth2.clientSecret={{ .Env.INSTILL_OAUTH_CLIENT_SECRET }}
2630
- -X main.updaterEnabled=instill-ai/cli

internal/config/config_type.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Config interface {
1414
UnsetHost(string) error
1515
Hosts() ([]string, error)
1616
HostsTyped() ([]HostConfigTyped, error)
17+
HostEntries() ([]*HostConfig, error)
1718
DefaultHostname() string
1819
CheckWriteable(string, string) error
1920
Write() error

internal/config/from_file.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,11 @@ func (c *fileConfig) UnsetHost(hostname string) error {
103103
cm := ConfigMap{hostsEntry.ValueNode}
104104
cm.RemoveEntry(hostname)
105105

106-
_, err = c.hostEntries()
107-
if strings.Contains(err.Error(), "could not find any host configurations") {
108-
// no hosts, fallback to the default hostname
109-
defaultHost := instance.FallbackHostname()
110-
err = c.Set("", "default_hostname", defaultHost)
111-
if err != nil {
112-
return err
113-
}
114-
}
115-
116106
return nil
117107
}
118108

119109
func (c *fileConfig) ConfigForHost(hostname string) (*HostConfig, error) {
120-
hosts, err := c.hostEntries()
110+
hosts, err := c.HostEntries()
121111
if err != nil {
122112
return nil, err
123113
}
@@ -167,7 +157,7 @@ func (c *fileConfig) Write() error {
167157
return WriteConfigFile(HostsConfigFile(), yamlNormalize(hostsBytes))
168158
}
169159

170-
func (c *fileConfig) hostEntries() ([]*HostConfig, error) {
160+
func (c *fileConfig) HostEntries() ([]*HostConfig, error) {
171161
entry, err := c.FindEntry("hosts")
172162
if err != nil {
173163
return []*HostConfig{}, nil
@@ -184,7 +174,7 @@ func (c *fileConfig) hostEntries() ([]*HostConfig, error) {
184174
// Hosts returns a list of all known hostnames configured in hosts.yml
185175
// TODO replace with HostsTyped
186176
func (c *fileConfig) Hosts() ([]string, error) {
187-
entries, err := c.hostEntries()
177+
entries, err := c.HostEntries()
188178
if err != nil {
189179
return nil, err
190180
}
@@ -203,7 +193,7 @@ func (c *fileConfig) Hosts() ([]string, error) {
203193
// Every call re-reads the config file.
204194
func (c *fileConfig) HostsTyped() ([]HostConfigTyped, error) {
205195
var ret []HostConfigTyped
206-
hosts, err := c.hostEntries()
196+
hosts, err := c.HostEntries()
207197
if err != nil {
208198
return nil, err
209199
}

internal/config/stub.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func (c ConfigStub) Hosts() ([]string, error) {
3636
return nil, nil
3737
}
3838

39+
func (c ConfigStub) HostEntries() ([]*HostConfig, error) {
40+
return nil, nil
41+
}
42+
3943
func (c ConfigStub) UnsetHost(hostname string) error {
4044
return nil
4145
}

internal/oauth2/auth_code_flow.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ import (
2626
)
2727

2828
var (
29+
// apiHostname is the default API hostname for the Instill Cloud server.
30+
apiHostname = ""
31+
// The OAuth2 hostname for the Instill Cloud server.
32+
oauth2Hostname = ""
33+
// The OAuth2 audience for the Instill Cloud server.
34+
oauth2Audience = ""
35+
// The OAuth2 issuer for the Instill Cloud server.
36+
oauth2Issuer = ""
2937
// The "Instill CLI" OAuth app (build-time default to api.instill.tech)
3038
clientID = ""
3139
// This value is safe to be embedded in version control (build-time default to api.instill.tech)
@@ -36,11 +44,11 @@ var (
3644
func HostConfigInstillCloud() *config.HostConfigTyped {
3745

3846
host := config.DefaultHostConfig()
39-
host.APIHostname = "api.instill.tech"
47+
host.APIHostname = apiHostname
4048
host.IsDefault = true
41-
host.Oauth2Hostname = "auth.instill.tech"
42-
host.Oauth2Audience = "https://api.instill.tech"
43-
host.Oauth2Issuer = "https://auth.instill.tech/"
49+
host.Oauth2Hostname = oauth2Hostname
50+
host.Oauth2Audience = oauth2Audience
51+
host.Oauth2Issuer = oauth2Issuer
4452
host.Oauth2ClientID = clientID
4553
host.Oauth2ClientSecret = clientSecret
4654
return &host

pkg/cmd/api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func NewCmdAPI(f *cmdutil.Factory, runF func(*ApiOptions) error) *cobra.Command
9797
$ inst api model/v1alpha/models
9898
9999
# get user profile
100-
$ inst api base/v1alpha/users/me
100+
$ inst api core/v1alpha/users/me
101101
102102
# add parameters to a GET request
103103
$ inst api model/v1alpha/models?visibility=public

pkg/cmd/auth/login/login.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,36 @@ func loginRun(f *cmdutil.Factory, opts *LoginOptions) error {
9292
return err
9393
}
9494
} else {
95-
hostname := opts.Hostname
96-
hosts, err := cfg.HostsTyped()
97-
if err != nil {
95+
// in case the hosts.yml is empty
96+
cfg, _ := opts.Config()
97+
if hosts, err := cfg.HostEntries(); err != nil {
9898
return err
99-
}
100-
for _, h := range hosts {
101-
if h.APIHostname == hostname {
102-
host = &h
103-
break
99+
} else if len(hosts) == 0 {
100+
// no hosts, fallback to the default hostname
101+
host = oauth2.HostConfigInstillCloud()
102+
err = cfg.SaveTyped(host)
103+
if err != nil {
104+
return err
105+
}
106+
err = cfg.Set("", "default_hostname", host.APIHostname)
107+
if err != nil {
108+
return err
109+
}
110+
} else {
111+
hostname := opts.Hostname
112+
hosts, err := cfg.HostsTyped()
113+
if err != nil {
114+
return err
115+
}
116+
for _, h := range hosts {
117+
if h.APIHostname == hostname {
118+
host = &h
119+
break
120+
}
121+
}
122+
if host == nil {
123+
return fmt.Errorf("ERROR: instance '%s' does not exists", hostname)
104124
}
105-
}
106-
if host == nil {
107-
return fmt.Errorf("ERROR: instance '%s' does not exists", hostname)
108125
}
109126
}
110127

@@ -171,7 +188,7 @@ type localLoginRequest struct {
171188

172189
// loginLocal handles dedicated auth flow for Instill Core.
173190
func loginLocal(transport http.RoundTripper, hostname, password string) (string, error) {
174-
url := instance.GetProtocol(hostname) + "base/v1alpha/auth/login"
191+
url := instance.GetProtocol(hostname) + "core/v1alpha/auth/login"
175192
data := &localLoginRequest{
176193
Name: local.DefUsername,
177194
Pass: password,

pkg/cmd/local/deploy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ func NewDeployCmd(f *cmdutil.Factory, runF func(*DeployOptions) error) *cobra.Co
7878

7979
cmd.Flags().BoolVarP(&opts.Force, "force", "f", false, "Force to deploy a new local Instill Core instance")
8080
cmd.Flags().BoolVarP(&opts.Upgrade, "upgrade", "u", false, "Upgrade Instill Core instance to the latest release version")
81+
cmd.Flags().BoolVarP(&opts.Build, "build", "b", false, "Deploy an Instill Core instance and build latest release version")
8182
cmd.Flags().BoolVarP(&opts.Latest, "latest", "l", false, "Deploy an Instill Core instance with the latest version (unstable)")
82-
cmd.Flags().BoolVarP(&opts.Build, "build", "b", false, "Deploy an Instill Core instance and build the images at the local")
8383
cmd.MarkFlagsMutuallyExclusive("force", "upgrade")
8484
cmd.MarkFlagsMutuallyExclusive("upgrade", "latest")
8585

@@ -242,7 +242,7 @@ func runDeploy(opts *DeployOptions) error {
242242

243243
if opts.Latest {
244244
p(opts.IO, "Spin up latest Instill Core...")
245-
if out, err := execCmd(opts.Exec, "bash", "-c", fmt.Sprintf("make latest BUILD=%s", strconv.FormatBool(opts.Build))); err != nil {
245+
if out, err := execCmd(opts.Exec, "bash", "-c", "make latest"); err != nil {
246246
return fmt.Errorf("ERROR: Instill Core spin-up failed, %w\n%s", err, out)
247247
}
248248
} else {

script/build.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ var tasks = map[string]func(string) error{
5353
ldflags = fmt.Sprintf("-X github.com/instill-ai/cli/internal/build.Version=%s %s", version(), ldflags)
5454
ldflags = fmt.Sprintf("-X github.com/instill-ai/cli/internal/build.Date=%s %s", date(), ldflags)
5555
if oauthSecret := os.Getenv("INSTILL_OAUTH_CLIENT_SECRET"); oauthSecret != "" {
56+
ldflags = fmt.Sprintf("-X github.com/instill-ai/cli/internal/oauth2.apiHostname=%s %s", os.Getenv("INSTILL_OAUTH_API_HOSTNAME"), ldflags)
57+
ldflags = fmt.Sprintf("-X github.com/instill-ai/cli/internal/oauth2.oauth2Hostname=%s %s", os.Getenv("INSTILL_OAUTH_OAUTH_HOSTNAME"), ldflags)
58+
ldflags = fmt.Sprintf("-X github.com/instill-ai/cli/internal/oauth2.oauth2Audience=%s %s", os.Getenv("INSTILL_OAUTH_OAUTH_AUDIENCE"), ldflags)
59+
ldflags = fmt.Sprintf("-X github.com/instill-ai/cli/internal/oauth2.oauth2Issuer=%s %s", os.Getenv("INSTILL_OAUTH_OAUTH_ISSUER"), ldflags)
5660
ldflags = fmt.Sprintf("-X github.com/instill-ai/cli/internal/oauth2.clientID=%s %s", os.Getenv("INSTILL_OAUTH_CLIENT_ID"), ldflags)
5761
ldflags = fmt.Sprintf("-X github.com/instill-ai/cli/internal/oauth2.clientSecret=%s %s", oauthSecret, ldflags)
5862
}

0 commit comments

Comments
 (0)