Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 1373030

Browse files
committed
Add ability to input a session token from a cli flag
1 parent 67eb3b4 commit 1373030

File tree

2 files changed

+45
-28
lines changed

2 files changed

+45
-28
lines changed

internal/cmd/agent.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,11 @@ func agentCmd() *cobra.Command {
4141

4242
func startCmd() *cobra.Command {
4343
var (
44-
token string
45-
coderURL string
46-
logFile string
47-
pullCert bool
44+
token string
45+
sessionToken string
46+
coderURL string
47+
logFile string
48+
pullCert bool
4849
)
4950
cmd := &cobra.Command{
5051
Use: "start --coder-url=<coder_url> --token=<token> --log-file=<path>",
@@ -107,9 +108,9 @@ coder agent start --log-file=/tmp/coder-agent.log
107108
}
108109
}
109110

110-
// First inject certs
111+
// First inject certs if enabled
111112
if pullCert {
112-
count, err := writeCoderdCerts(ctx)
113+
count, err := writeCoderdCerts(ctx, sessionToken)
113114
if err != nil {
114115
return xerrors.Errorf("trust certs: %w", err)
115116
}
@@ -139,16 +140,17 @@ coder agent start --log-file=/tmp/coder-agent.log
139140
}
140141

141142
cmd.Flags().StringVar(&token, "token", "", "coder agent token")
143+
cmd.Flags().StringVar(&sessionToken, "session-token", "", "coder session token to auth as user")
142144
cmd.Flags().StringVar(&coderURL, "coder-url", "", "coder access url")
143145
cmd.Flags().StringVar(&logFile, "log-file", "", "write a copy of logs to file")
144146
cmd.Flags().BoolVar(&pullCert, "pull-cert", true, "pulls the tls certificate from coderd to ensure the cert is trusted")
145147

146148
return cmd
147149
}
148150

149-
func writeCoderdCerts(ctx context.Context) (int, error) {
151+
func writeCoderdCerts(ctx context.Context, sessionToken string) (int, error) {
150152
// Inject certs to custom dir and concat with : with existing dir.
151-
certs, err := trustCertificate(ctx)
153+
certs, err := trustCertificate(ctx, sessionToken)
152154
if err != nil {
153155
return 0, xerrors.Errorf("trust cert: %w", err)
154156
}
@@ -187,7 +189,7 @@ func writeCoderdCerts(ctx context.Context) (int, error) {
187189
// It will then extend the certs to trust to include this directory.
188190
// This only happens if coderd can answer the challenge to prove
189191
// it has the shared secret.
190-
func trustCertificate(ctx context.Context) ([][]byte, error) {
192+
func trustCertificate(ctx context.Context, sessionToken string) ([][]byte, error) {
191193
conf := &tls.Config{InsecureSkipVerify: true}
192194
hc := &http.Client{
193195
Timeout: time.Second * 3,
@@ -196,7 +198,7 @@ func trustCertificate(ctx context.Context) ([][]byte, error) {
196198
},
197199
}
198200

199-
c, err := newClient(ctx, false, withHTTPClient(hc))
201+
c, err := newClient(ctx, false, withHTTPClient(hc), withSessionToken(sessionToken))
200202
if err != nil {
201203
return nil, xerrors.Errorf("new client: %w", err)
202204
}

internal/cmd/auth.go

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ const urlEnv = "CODER_URL"
2525

2626
type coderOpt func(opt *coder.ClientOptions)
2727

28+
func withSessionToken(token string) coderOpt {
29+
return func(opt *coder.ClientOptions) {
30+
opt.Token = token
31+
}
32+
}
33+
2834
func withHTTPClient(hc *http.Client) coderOpt {
2935
return func(opt *coder.ClientOptions) {
3036
opt.HTTPClient = hc
@@ -38,30 +44,39 @@ func newClient(ctx context.Context, checkVersion bool, optsF ...coderOpt) (coder
3844
rawURL = os.Getenv(urlEnv)
3945
)
4046

41-
if sessionToken == "" || rawURL == "" {
42-
sessionToken, err = config.Session.Read()
43-
if err != nil {
44-
return nil, errNeedLogin
45-
}
47+
clientOpts := &coder.ClientOptions{}
48+
for _, f := range optsF {
49+
f(clientOpts)
50+
}
4651

47-
rawURL, err = config.URL.Read()
48-
if err != nil {
49-
return nil, errNeedLogin
52+
// Missing the token
53+
if clientOpts.Token == "" {
54+
// If the env var is not set, try the config file
55+
if sessionToken == "" {
56+
sessionToken, err = config.Session.Read()
57+
if err != nil {
58+
return nil, errNeedLogin
59+
}
5060
}
61+
clientOpts.Token = sessionToken
5162
}
5263

53-
u, err := url.Parse(rawURL)
54-
if err != nil {
55-
return nil, xerrors.Errorf("url malformed: %w try running \"coder login\" with a valid URL", err)
56-
}
64+
// Missing the url
65+
if clientOpts.BaseURL == nil {
66+
// If the env var is not set, try the config file
67+
if rawURL == "" {
68+
rawURL, err = config.URL.Read()
69+
if err != nil {
70+
return nil, errNeedLogin
71+
}
72+
}
5773

58-
clientOpts := &coder.ClientOptions{
59-
BaseURL: u,
60-
Token: sessionToken,
61-
}
74+
u, err := url.Parse(rawURL)
75+
if err != nil {
76+
return nil, xerrors.Errorf("url malformed: %w try running \"coder login\" with a valid URL", err)
77+
}
6278

63-
for _, f := range optsF {
64-
f(clientOpts)
79+
clientOpts.BaseURL = u
6580
}
6681

6782
c, err := coder.NewClient(*clientOpts)

0 commit comments

Comments
 (0)