Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit 72bebd9

Browse files
authored
Feature: Console Login Command (#88)
1 parent 5ef5b4a commit 72bebd9

File tree

5 files changed

+88
-11
lines changed

5 files changed

+88
-11
lines changed

cmd/console.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2020 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"path"
21+
22+
"github.com/spf13/cobra"
23+
24+
"github.com/netflix/weep/pkg/config"
25+
"github.com/netflix/weep/pkg/util"
26+
)
27+
28+
func init() {
29+
consoleCmd.PersistentFlags().BoolVarP(&noOpen, "no-open", "x", false, "print the link, but do not open a browser window")
30+
rootCmd.AddCommand(consoleCmd)
31+
}
32+
33+
var consoleCmd = &cobra.Command{
34+
Use: "console",
35+
Short: consoleShortHelp,
36+
Long: consoleLongHelp,
37+
Args: cobra.MaximumNArgs(1),
38+
RunE: runConsole,
39+
}
40+
41+
func runConsole(cmd *cobra.Command, args []string) error {
42+
// If a role was provided, use it, otherwise prompt
43+
role, err := InteractiveRolePrompt(args, region, nil)
44+
if err != nil {
45+
return err
46+
}
47+
48+
// Construct the URL and open/print it; default to HTTPS if not specified
49+
base_url := config.BaseWebURL()
50+
url := path.Join(base_url, "role", role)
51+
52+
if noOpen {
53+
cmd.Println(url)
54+
} else {
55+
err := util.OpenLink(url)
56+
if err != nil {
57+
return err
58+
}
59+
}
60+
61+
return nil
62+
}

cmd/vars.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ var completionLongHelp = `Generate shell completion script for Bash, Zsh, Fish,
4747
4848
More information: https://hawkins.gitbook.io/consoleme/weep-cli/advanced-configuration/shell-completion
4949
`
50+
51+
var consoleShortHelp = "Log into the AWS Management console"
52+
var consoleLongHelp = `The login command opens a browser window with a link that will log you into the
53+
AWS Management console using the specified role. You can use the --no-open flag to simply print the console
54+
link, rather than opening it in a browser.
55+
`
56+
5057
var credentialProcessShortHelp = "Retrieve credentials on the fly via the AWS SDK"
5158
var credentialProcessLongHelp = `The credential_process command can be used by AWS SDKs to retrieve
5259
credentials from Weep on the fly. The --generate flag lets you automatically

pkg/config/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ func MtlsEnabled() bool {
164164
return authMethod == "mtls"
165165
}
166166

167+
// BaseWebURL allows the ConsoleMe URL to be overridden for cases where the API
168+
// and UI are accessed via different URLs
169+
func BaseWebURL() string {
170+
if override := viper.GetString("consoleme_open_url_override"); override != "" {
171+
return override
172+
}
173+
return viper.GetString("consoleme_url")
174+
}
175+
167176
var (
168177
Config WeepConfig
169178
)

pkg/creds/consoleme.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import (
3131
"time"
3232

3333
"github.com/netflix/weep/pkg/aws"
34-
34+
"github.com/netflix/weep/pkg/config"
3535
werrors "github.com/netflix/weep/pkg/errors"
3636
"github.com/netflix/weep/pkg/httpAuth/challenge"
3737
"github.com/netflix/weep/pkg/httpAuth/mtls"
@@ -207,16 +207,7 @@ func (c *Client) GetResourceURL(arn string) (string, error) {
207207
if err := json.Unmarshal(document, &responseParsed); err != nil {
208208
return "", errors.Wrap(err, "failed to unmarshal JSON")
209209
}
210-
return baseWebURL() + responseParsed.Data["url"], nil
211-
}
212-
213-
// baseWebURL allows the ConsoleMe URL to be overridden for cases where the API
214-
// and UI are accessed via different URLs
215-
func baseWebURL() string {
216-
if override := viper.GetString("consoleme_open_url_override"); override != "" {
217-
return override
218-
}
219-
return viper.GetString("consoleme_url")
210+
return config.BaseWebURL() + responseParsed.Data["url"], nil
220211
}
221212

222213
func parseWebError(rawErrorResponse []byte) error {

pkg/util/util.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ func OpenLink(link string) error {
151151
return errors.BrowserOpenError
152152
}
153153

154+
// If the user specified additional arguments to pass to the program, parse and insert those now
155+
opts := os.Getenv("WEEP_OPEN_LINK_OPTIONS")
156+
if opts != "" {
157+
for _, opt := range strings.Split(opts, ",") {
158+
openUrlCommand = append(openUrlCommand, opt)
159+
}
160+
}
161+
154162
if openUrlCommand != nil {
155163
cmd := exec.Command(openUrlCommand[0], append(openUrlCommand[1:], link)...)
156164
err := cmd.Start()

0 commit comments

Comments
 (0)