Skip to content

Commit

Permalink
Merge pull request #227 from bomoko/feature/get_token
Browse files Browse the repository at this point in the history
Get and print auth token
  • Loading branch information
shreddedbacon authored Jun 3, 2022
2 parents 995f2aa + 209ae08 commit d24c113
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
12 changes: 12 additions & 0 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ var getProjectKeyCmd = &cobra.Command{
},
}

var getToken = &cobra.Command{
Use: "token",
Aliases: []string{"tk"},
Short: "Generates a Lagoon auth token (for use in, for example, graphQL queries)",
Run: func(cmd *cobra.Command, args []string) {
token, err := retrieveTokenViaSsh()
handleError(err)
fmt.Println(token)
},
}

func init() {
getCmd.AddCommand(getAllUserKeysCmd)
getCmd.AddCommand(getDeploymentCmd)
Expand All @@ -163,6 +174,7 @@ func init() {
getCmd.AddCommand(getProjectKeyCmd)
getCmd.AddCommand(getUserKeysCmd)
getCmd.AddCommand(getTaskByID)
getCmd.AddCommand(getToken)
getTaskByID.Flags().IntP("id", "I", 0, "ID of the task")
getTaskByID.Flags().BoolP("logs", "L", false, "Show the task logs if available")
getProjectKeyCmd.Flags().BoolVarP(&revealValue, "reveal", "", false, "Reveal the variable values")
Expand Down
32 changes: 20 additions & 12 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ func publicKey(path string, skipAgent bool) (ssh.AuthMethod, func() error) {
}

func loginToken() error {
out, err := retrieveTokenViaSsh()
if err != nil {
return err
}

lc := lagoonCLIConfig.Lagoons[lagoonCLIConfig.Current]
lc.Token = out
lagoonCLIConfig.Lagoons[lagoonCLIConfig.Current] = lc
if err = writeLagoonConfig(&lagoonCLIConfig, filepath.Join(configFilePath, configName+configExtension)); err != nil {
return fmt.Errorf("couldn't write config: %v", err)
}

return nil
}

func retrieveTokenViaSsh() (string, error) {
skipAgent := false
privateKey := fmt.Sprintf("%s/.ssh/id_rsa", userPath)
if cmdSSHKey != "" {
Expand All @@ -86,26 +102,18 @@ func loginToken() error {
lagoonCLIConfig.Lagoons[lagoonCLIConfig.Current].Port)
conn, err := ssh.Dial("tcp", sshHost, config)
if err != nil {
return fmt.Errorf("couldn't connect to %s: %v", sshHost, err)
return "", fmt.Errorf("couldn't connect to %s: %v", sshHost, err)
}
defer conn.Close()

session, err := conn.NewSession()
if err != nil {
return fmt.Errorf("couldn't open session: %v", err)
return "", fmt.Errorf("couldn't open session: %v", err)
}

out, err := session.CombinedOutput("token")
if err != nil {
return fmt.Errorf("couldn't get token: %v", err)
}

lc := lagoonCLIConfig.Lagoons[lagoonCLIConfig.Current]
lc.Token = strings.TrimSpace(string(out))
lagoonCLIConfig.Lagoons[lagoonCLIConfig.Current] = lc
if err = writeLagoonConfig(&lagoonCLIConfig, filepath.Join(configFilePath, configName+configExtension)); err != nil {
return fmt.Errorf("couldn't write config: %v", err)
return "", fmt.Errorf("couldn't get token: %v", err)
}

return nil
return strings.TrimSpace(string(out)), err
}
1 change: 1 addition & 0 deletions docs/commands/lagoon_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ Get info on a resource
* [lagoon get project-key](lagoon_get_project-key.md) - Get a projects public key
* [lagoon get project-metadata](lagoon_get_project-metadata.md) - Get all metadata for a project
* [lagoon get task-by-id](lagoon_get_task-by-id.md) - Get information about a task by its ID
* [lagoon get token](lagoon_get_token.md) - Generates a Lagoon auth token (for use in, for example, graphQL queries)
* [lagoon get user-sshkeys](lagoon_get_user-sshkeys.md) - Get a user's SSH keys

39 changes: 39 additions & 0 deletions docs/commands/lagoon_get_token.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## lagoon get token

Generates a Lagoon auth token (for use in, for example, graphQL queries)

### Synopsis

Generates a Lagoon auth token (for use in, for example, graphQL queries)

```
lagoon get token [flags]
```

### Options

```
-h, --help help for token
```

### Options inherited from parent commands

```
--config-file string Path to the config file to use (must be *.yml or *.yaml)
--debug Enable debugging output (if supported)
-e, --environment string Specify an environment to use
--force Force yes on prompts (if supported)
-l, --lagoon string The Lagoon instance to interact with
--no-header No header on table (if supported)
--output-csv Output as CSV (if supported)
--output-json Output as JSON (if supported)
--pretty Make JSON pretty (if supported)
-p, --project string Specify a project to use
--skip-update-check Skip checking for updates
-i, --ssh-key string Specify path to a specific SSH key to use for lagoon authentication
```

### SEE ALSO

* [lagoon get](lagoon_get.md) - Get info on a resource

0 comments on commit d24c113

Please sign in to comment.