Skip to content

Commit

Permalink
Merge pull request #35 from amazeeio/project-key
Browse files Browse the repository at this point in the history
Get project keys
  • Loading branch information
shreddedbacon authored Dec 3, 2019
2 parents 6022e7b + 7f147bd commit bdc40d7
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
32 changes: 32 additions & 0 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,41 @@ var getEnvironmentCmd = &cobra.Command{
},
}

var getProjectKeyCmd = &cobra.Command{
Use: "project-key",
Short: "Get a projects key",
Run: func(cmd *cobra.Command, args []string) {
getProjectFlags := parseGetFlags(*cmd.Flags())
if getProjectFlags.Project == "" {
fmt.Println("Not enough arguments. Requires: project name")
cmd.Help()
os.Exit(1)
}
returnedJSON, err := projects.GetProjectKey(getProjectFlags.Project, revealValue)
if err != nil {
output.RenderError(err.Error(), outputOptions)
os.Exit(1)
}
var dataMain output.Table
err = json.Unmarshal([]byte(returnedJSON), &dataMain)
if err != nil {
output.RenderError(err.Error(), outputOptions)
os.Exit(1)
}
if len(dataMain.Data) == 0 {
output.RenderError("no data returned", outputOptions)
os.Exit(1)
}
output.RenderOutput(dataMain, outputOptions)

},
}

func init() {
getCmd.AddCommand(getProjectCmd)
getCmd.AddCommand(getDeploymentCmd)
getCmd.AddCommand(getEnvironmentCmd)
getCmd.AddCommand(getProjectKeyCmd)
getProjectKeyCmd.Flags().BoolVarP(&revealValue, "reveal", "", false, "Reveal the variable values")
getDeploymentCmd.Flags().StringVarP(&remoteID, "remoteid", "R", "", "The remote ID of the deployment")
}
1 change: 1 addition & 0 deletions docs/commands/lagoon_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ Get info on a project, or deployment
* [lagoon get deployment](lagoon_get_deployment.md) - Get build log by remote id
* [lagoon get environment](lagoon_get_environment.md) - Details about an environment
* [lagoon get project](lagoon_get_project.md) - Details about a project
* [lagoon get project-key](lagoon_get_project-key.md) - Get a projects key

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

Get a projects key

### Synopsis

Get a projects key

```
lagoon get project-key [flags]
```

### Options

```
-h, --help help for project-key
--reveal Reveal the variable values
```

### Options inherited from parent commands

```
--all-projects All projects (if supported)
-e, --environment string Specify an environment to use
--force Force (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
-i, --ssh-key string Specify a specific SSH key to use
--version Version information
```

### SEE ALSO

* [lagoon get](lagoon_get.md) - Get info on a project, or deployment

58 changes: 58 additions & 0 deletions lagoon/projects/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package projects
import (
"encoding/json"
"fmt"
"strings"

"github.com/amazeeio/lagoon-cli/api"
"github.com/amazeeio/lagoon-cli/graphql"
"github.com/amazeeio/lagoon-cli/output"
"golang.org/x/crypto/ssh"
)

// ListAllProjects will list all projects
Expand Down Expand Up @@ -263,3 +265,59 @@ func processProjectUpdate(projectByName []byte, jsonPatch string) (api.UpdatePro
}
return projectUpdate, nil
}

// GetProjectKey will get basic info about a project
func GetProjectKey(projectName string, revealValue bool) ([]byte, error) {
// set up a lagoonapi client
lagoonAPI, err := graphql.LagoonAPI()
if err != nil {
return []byte(""), err
}
// get project info from lagoon
project := api.Project{
Name: projectName,
}
keyFragment := `fragment Project on Project {
privateKey
}`
projectByName, err := lagoonAPI.GetProjectByName(project, keyFragment)
if err != nil {
return []byte(""), err
}
returnResult, err := processProjectKey(projectByName, revealValue)
if err != nil {
return []byte(""), err
}
return returnResult, nil
}

func processProjectKey(projectByName []byte, revealValue bool) ([]byte, error) {
var project api.Project
err := json.Unmarshal([]byte(projectByName), &project)
if err != nil {
return []byte(""), err
}
signer, err := ssh.ParsePrivateKey([]byte(project.PrivateKey))
if err != nil {
fmt.Println("Error was:", err.Error())
return []byte(""), err
}
publicKey := signer.PublicKey()
// get the key, but strip the newlines we don't need
projectData := []string{
strings.TrimSuffix(string(ssh.MarshalAuthorizedKey(publicKey)), "\n"),
}
if revealValue {
projectData = append(projectData, strings.TrimSuffix(project.PrivateKey, "\n"))
}
var data []output.Data
data = append(data, projectData)
dataMain := output.Table{
Header: []string{"PublicKey"},
Data: data,
}
if revealValue {
dataMain.Header = append(dataMain.Header, "PrivateKey")
}
return json.Marshal(dataMain)
}

0 comments on commit bdc40d7

Please sign in to comment.