Skip to content

Commit aac3f20

Browse files
authored
added redirect-url to interactive mode (#661)
1 parent d7f1c16 commit aac3f20

File tree

7 files changed

+90
-16
lines changed

7 files changed

+90
-16
lines changed

docs/book/src/cli/convert-kubeconfig.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Flags:
3434
--password string password for ropc login flow. It may be specified in AAD_USER_PRINCIPAL_PASSWORD or AZURE_PASSWORD environment variable
3535
--pop-claims key=val,key2=val2 contains a comma-separated list of claims to attach to the pop token in the format key=val,key2=val2. At minimum, specify the ARM ID of the cluster as `u=ARM_ID`
3636
--pop-enabled set to true to use a PoP token for authentication or false to use a regular bearer token
37+
--redirect-url string The URL Microsoft Entra ID will redirect to with the access token. This is only used for interactive login. This is an optional parameter.
3738
--server-id string AAD server application ID
3839
-t, --tenant-id string AAD tenant ID. It may be specified in AZURE_TENANT_ID environment variable
3940
--timeout duration Timeout duration for Azure CLI token requests. It may be specified in AZURE_CLI_TIMEOUT environment variable (default 30s)

docs/book/src/cli/get-token.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Flags:
2929
--password string password for ropc login flow. It may be specified in AAD_USER_PRINCIPAL_PASSWORD or AZURE_PASSWORD environment variable
3030
--pop-claims key=val,key2=val2 contains a comma-separated list of claims to attach to the pop token in the format key=val,key2=val2. At minimum, specify the ARM ID of the cluster as `u=ARM_ID`
3131
--pop-enabled set to true to use a PoP token for authentication or false to use a regular bearer token
32+
--redirect-url string The URL Microsoft Entra ID will redirect to with the access token. This is only used for interactive login. This is an optional parameter.
3233
--server-id string AAD server application ID
3334
-t, --tenant-id string AAD tenant ID. It may be specified in AZURE_TENANT_ID environment variable
3435
--timeout duration Timeout duration for Azure CLI token requests. It may be specified in AZURE_CLI_TIMEOUT environment variable (default 30s)

docs/book/src/concepts/login-modes/interactive.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Web Browser Interactive
22

33
This login mode will automatically open a browser to login the user.
4-
Once authenticated, the browser will redirect back to a local web server with the credentials.
4+
Once authenticated, the browser will redirect back to a local web server with access token.
5+
The redirect URL can be set via `--redirect-url`.
56
This login mode complies with Conditional Access policy.
67

78
## Usage Examples
89

910
### Bearer token with interactive flow
11+
1012
```sh
1113
export KUBECONFIG=/path/to/kubeconfig
1214

@@ -15,7 +17,19 @@ kubelogin convert-kubeconfig -l interactive
1517
kubectl get nodes
1618
```
1719

20+
### Specifying Redirect URL
21+
22+
```sh
23+
export KUBECONFIG=/path/to/kubeconfig
24+
25+
kubelogin convert-kubeconfig -l interactive --redirect-url http://localhost:8080
26+
27+
kubectl get nodes
28+
```
29+
30+
1831
### Proof-of-possession (PoP) token with interactive flow
32+
1933
```sh
2034
export KUBECONFIG=/path/to/kubeconfig
2135

pkg/internal/converter/convert.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const (
3737
argIsPoPTokenEnabled = "--pop-enabled"
3838
argPoPTokenClaims = "--pop-claims"
3939
argDisableEnvironmentOverride = "--disable-environment-override"
40+
argRedirectURL = "--redirect-url"
4041

4142
flagAzureConfigDir = "azure-config-dir"
4243
flagClientID = "client-id"
@@ -59,6 +60,7 @@ const (
5960
flagIsPoPTokenEnabled = "pop-enabled"
6061
flagPoPTokenClaims = "pop-claims"
6162
flagDisableEnvironmentOverride = "disable-environment-override"
63+
flagRedirectURL = "redirect-url"
6264

6365
execName = "kubelogin"
6466
getTokenCommand = "get-token"
@@ -78,7 +80,8 @@ func getArgValues(o Options, authInfo *api.AuthInfo) (
7880
argEnvironmentVal,
7981
argTenantIDVal,
8082
argAuthRecordCacheDirVal,
81-
argPoPTokenClaimsVal string,
83+
argPoPTokenClaimsVal,
84+
argRedirectURLVal string,
8285
argIsLegacyConfigModeVal,
8386
argIsPoPTokenEnabledVal bool,
8487
) {
@@ -164,6 +167,12 @@ func getArgValues(o Options, authInfo *api.AuthInfo) (
164167
argPoPTokenClaimsVal = getExecArg(authInfo, argPoPTokenClaims)
165168
}
166169

170+
if o.isSet(flagRedirectURL) {
171+
argRedirectURLVal = o.TokenOptions.RedirectURL
172+
} else {
173+
argRedirectURLVal = getExecArg(authInfo, argRedirectURL)
174+
}
175+
167176
return
168177
}
169178

@@ -233,7 +242,15 @@ func Convert(o Options, pathOptions *clientcmd.PathOptions) error {
233242

234243
klog.V(5).Info("converting...")
235244

236-
argServerIDVal, argClientIDVal, argEnvironmentVal, argTenantIDVal, argAuthRecordCacheDirVal, argPoPTokenClaimsVal, isLegacyConfigMode, isPoPTokenEnabled := getArgValues(o, authInfo)
245+
argServerIDVal,
246+
argClientIDVal,
247+
argEnvironmentVal,
248+
argTenantIDVal,
249+
argAuthRecordCacheDirVal,
250+
argPoPTokenClaimsVal,
251+
argRedirectURLVal,
252+
isLegacyConfigMode,
253+
isPoPTokenEnabled := getArgValues(o, authInfo)
237254
exec := &api.ExecConfig{
238255
Command: execName,
239256
Args: []string{
@@ -328,6 +345,10 @@ func Convert(o Options, pathOptions *clientcmd.PathOptions) error {
328345
return err
329346
}
330347

348+
if argRedirectURLVal != "" {
349+
exec.Args = append(exec.Args, argRedirectURL, argRedirectURLVal)
350+
}
351+
331352
case token.ServicePrincipalLogin:
332353

333354
if argClientIDVal == "" {

pkg/internal/converter/convert_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func TestConvert(t *testing.T) {
3232
federatedTokenFile = "/tmp/file"
3333
authRecordCacheDir = "/tmp/token_dir"
3434
azureCLIDir = "/tmp/foo"
35+
redirectURL = "http://localhost:8000"
3536
)
3637
testData := []struct {
3738
name string
@@ -1208,6 +1209,35 @@ func TestConvert(t *testing.T) {
12081209
},
12091210
command: execName,
12101211
},
1212+
{
1213+
name: "with exec format kubeconfig, convert from devicecode to interactive with redirect url override",
1214+
execArgItems: []string{
1215+
getTokenCommand,
1216+
argServerID, serverID,
1217+
argClientID, clientID,
1218+
argTenantID, tenantID,
1219+
argEnvironment, envName,
1220+
argLoginMethod, token.DeviceCodeLogin,
1221+
},
1222+
overrideFlags: map[string]string{
1223+
flagLoginMethod: token.InteractiveLogin,
1224+
flagServerID: serverID,
1225+
flagClientID: clientID,
1226+
flagTenantID: tenantID,
1227+
flagEnvironment: envName,
1228+
flagRedirectURL: redirectURL,
1229+
},
1230+
expectedArgs: []string{
1231+
getTokenCommand,
1232+
argServerID, serverID,
1233+
argClientID, clientID,
1234+
argTenantID, tenantID,
1235+
argEnvironment, envName,
1236+
argLoginMethod, token.InteractiveLogin,
1237+
argRedirectURL, redirectURL,
1238+
},
1239+
command: execName,
1240+
},
12111241
{
12121242
name: "convert with context specified, auth info not specified by the context should not be changed",
12131243
authProviderConfig: map[string]string{

pkg/internal/token/interactivebrowsercredential.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func newInteractiveBrowserCredential(opts *Options, record azidentity.Authentica
4242
ClientID: opts.ClientID,
4343
TenantID: opts.TenantID,
4444
DisableInstanceDiscovery: opts.DisableInstanceDiscovery,
45+
RedirectURL: opts.RedirectURL,
4546
}
4647

4748
if opts.httpClient != nil {

pkg/internal/token/options.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Options struct {
4141
UsePersistentCache bool
4242
DisableInstanceDiscovery bool
4343
httpClient *http.Client
44+
RedirectURL string
4445
}
4546

4647
const (
@@ -119,6 +120,7 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {
119120
fs.StringVar(&o.PoPTokenClaims, "pop-claims", o.PoPTokenClaims, "contains a comma-separated list of claims to attach to the pop token in the format `key=val,key2=val2`. At minimum, specify the ARM ID of the cluster as `u=ARM_ID`")
120121
fs.BoolVar(&o.DisableEnvironmentOverride, "disable-environment-override", o.DisableEnvironmentOverride, "Enable or disable the use of env-variables. Default false")
121122
fs.BoolVar(&o.DisableInstanceDiscovery, "disable-instance-discovery", o.DisableInstanceDiscovery, "set to true to disable instance discovery in environments with their own simple Identity Provider (not AAD) that do not have instance metadata discovery endpoint. Default false")
123+
fs.StringVar(&o.RedirectURL, "redirect-url", o.RedirectURL, "The URL Microsoft Entra ID will redirect to with the access token. This is only used for interactive login. This is an optional parameter.")
122124
}
123125

124126
func (o *Options) Validate() error {
@@ -275,19 +277,23 @@ func (o *Options) GetCloudConfiguration() cloud.Configuration {
275277

276278
func (o *Options) ToString() string {
277279
azureConfigDir := os.Getenv("AZURE_CONFIG_DIR")
278-
return fmt.Sprintf("Login Method: %s, Environment: %s, TenantID: %s, ServerID: %s, ClientID: %s, IsLegacy: %t, msiResourceID: %s, Timeout: %v, authRecordCacheDir: %s, tokenauthRecordFile: %s, AZURE_CONFIG_DIR: %s",
279-
o.LoginMethod,
280-
o.Environment,
281-
o.TenantID,
282-
o.ServerID,
283-
o.ClientID,
284-
o.IsLegacy,
285-
o.IdentityResourceID,
286-
o.Timeout,
287-
o.AuthRecordCacheDir,
288-
o.authRecordCacheFile,
289-
azureConfigDir,
290-
)
280+
281+
parts := []string{
282+
fmt.Sprintf("Login Method: %s", o.LoginMethod),
283+
fmt.Sprintf("Environment: %s", o.Environment),
284+
fmt.Sprintf("TenantID: %s", o.TenantID),
285+
fmt.Sprintf("ServerID: %s", o.ServerID),
286+
fmt.Sprintf("ClientID: %s", o.ClientID),
287+
fmt.Sprintf("IsLegacy: %t", o.IsLegacy),
288+
fmt.Sprintf("msiResourceID: %s", o.IdentityResourceID),
289+
fmt.Sprintf("Timeout: %v", o.Timeout),
290+
fmt.Sprintf("authRecordCacheDir: %s", o.AuthRecordCacheDir),
291+
fmt.Sprintf("tokenauthRecordFile: %s", o.authRecordCacheFile),
292+
fmt.Sprintf("AZURE_CONFIG_DIR: %s", azureConfigDir),
293+
fmt.Sprintf("RedirectURL: %s", o.RedirectURL),
294+
}
295+
296+
return strings.Join(parts, ", ")
291297
}
292298

293299
func getAuthenticationRecordFileName(o *Options) string {

0 commit comments

Comments
 (0)