Skip to content

Commit

Permalink
feat: initial support for ssh-portal in cli (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Jun 2, 2023
1 parent dbd7b87 commit 85daffe
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 18 deletions.
17 changes: 9 additions & 8 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@ import (

// LagoonConfigFlags .
type LagoonConfigFlags struct {
Lagoon string `json:"lagoon,omitempty"`
Hostname string `json:"hostname,omitempty"`
Port string `json:"port,omitempty"`
GraphQL string `json:"graphql,omitempty"`
Token string `json:"token,omitempty"`
UI string `json:"ui,omitempty"`
Kibana string `json:"kibana,omitempty"`
SSHKey string `json:"sshkey,omitempty"`
Lagoon string `json:"lagoon,omitempty"`
Hostname string `json:"hostname,omitempty"`
Port string `json:"port,omitempty"`
GraphQL string `json:"graphql,omitempty"`
Token string `json:"token,omitempty"`
UI string `json:"ui,omitempty"`
Kibana string `json:"kibana,omitempty"`
SSHKey string `json:"sshkey,omitempty"`
SSHPortal bool `json:"sshportal,omitempty"`
}

func parseLagoonConfig(flags pflag.FlagSet) LagoonConfigFlags {
Expand Down
41 changes: 39 additions & 2 deletions cmd/ssh.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cmd

import (
"context"
"fmt"

"github.com/spf13/cobra"
"github.com/uselagoon/lagoon-cli/internal/lagoon"
"github.com/uselagoon/lagoon-cli/internal/lagoon/client"
lagoonssh "github.com/uselagoon/lagoon-cli/pkg/lagoon/ssh"
"github.com/uselagoon/lagoon-cli/pkg/output"
"golang.org/x/crypto/ssh"
Expand All @@ -23,12 +26,46 @@ var sshEnvCmd = &cobra.Command{
if cmdProjectName == "" || cmdProjectEnvironment == "" {
return fmt.Errorf("Missing arguments: Project name or environment name are not defined")
}
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}

// allow the use of the `feature/branch` and standard `feature-branch` type environment names to be used
// since ssh requires the `feature-branch` type name to be used as the ssh username
// run the environment through the makesafe and shorted functions that lagoon uses
environmentName := makeSafe(shortenEnvironment(cmdProjectName, cmdProjectEnvironment))

current := lagoonCLIConfig.Current
// set the default ssh host and port to the core ssh endpoint
sshHost := lagoonCLIConfig.Lagoons[current].HostName
sshPort := lagoonCLIConfig.Lagoons[current].Port

// if the config for this lagoon is set to use ssh portal support, handle that here
if lagoonCLIConfig.Lagoons[current].SSHPortal {
lc := client.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIConfig.Lagoons[current].Token,
lagoonCLIConfig.Lagoons[current].Version,
lagoonCLIVersion,
debug)
project, err := lagoon.GetSSHEndpointsByProject(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
// check all the environments for this project
for _, env := range project.Environments {
// if the env name matches the requested environment then check if the deploytarget supports regional ssh endpoints
if env.Name == environmentName {
// if the deploytarget supports regional endpoints, then set these as the host and port for ssh
if env.DeployTarget.SSHHost != "" && env.DeployTarget.SSHPort != "" {
sshHost = env.DeployTarget.SSHHost
sshPort = env.DeployTarget.SSHPort
}
}
}
}

// get private key that the cli is using
skipAgent := false

Expand All @@ -44,8 +81,8 @@ var sshEnvCmd = &cobra.Command{
skipAgent = true
}
sshConfig := map[string]string{
"hostname": lagoonCLIConfig.Lagoons[lagoonCLIConfig.Current].HostName,
"port": lagoonCLIConfig.Lagoons[lagoonCLIConfig.Current].Port,
"hostname": sshHost,
"port": sshPort,
"username": cmdProjectName + "-" + environmentName,
"sshkey": privateKey,
}
Expand Down
16 changes: 16 additions & 0 deletions internal/lagoon/client/_lgraphql/sshEndpointsByProject.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
query ($name: String!) {
projectByName(name: $name) {
id
name
environments {
id
name
openshiftProjectName
openshift{
id
sshHost
sshPort
}
}
}
}
23 changes: 23 additions & 0 deletions internal/lagoon/client/lgraphql/lgraphql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions internal/lagoon/client/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,26 @@ func (c *Client) DeployTargetConfigsByProjectID(
})
}

// SSHEndpointsByProject queries the Lagoon API for a project by its name, and
// unmarshals the response into project.
func (c *Client) SSHEndpointsByProject(
ctx context.Context, name string, project *schema.Project) error {

req, err := c.newVersionedRequest("_lgraphql/sshEndpointsByProject.graphql",
map[string]interface{}{
"name": name,
})
if err != nil {
return err
}

return c.client.Run(ctx, req, &struct {
Response *schema.Project `json:"projectByName"`
}{
Response: project,
})
}

// ListDeployTargets queries the Lagoon API for a deploytargets and unmarshals the response into deploytargets.
func (c *Client) ListDeployTargets(
ctx context.Context, deploytargets *[]schema.DeployTarget) error {
Expand Down
17 changes: 9 additions & 8 deletions internal/lagoon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ type Config struct {

// Context is used for each lagoon context in the config file.
type Context struct {
GraphQL string `json:"graphql"`
HostName string `json:"hostname"`
UI string `json:"ui,omitempty"`
Kibana string `json:"kibana,omitempty"`
Port string `json:"port"`
Token string `json:"token,omitempty"`
Version string `json:"version,omitempty"`
SSHKey string `json:"sshkey,omitempty"`
GraphQL string `json:"graphql"`
HostName string `json:"hostname"`
UI string `json:"ui,omitempty"`
Kibana string `json:"kibana,omitempty"`
Port string `json:"port"`
Token string `json:"token,omitempty"`
Version string `json:"version,omitempty"`
SSHKey string `json:"sshkey,omitempty"`
SSHPortal bool `json:"sshPortal,omitempty"`
}
7 changes: 7 additions & 0 deletions internal/lagoon/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Projects interface {
ProjectsByMetadata(ctx context.Context, key string, value string, project *[]schema.ProjectMetadata) error
UpdateProjectMetadata(ctx context.Context, id int, key string, value string, project *schema.ProjectMetadata) error
RemoveProjectMetadataByKey(ctx context.Context, id int, key string, project *schema.ProjectMetadata) error
SSHEndpointsByProject(ctx context.Context, name string, project *schema.Project) error
}

// GetMinimalProjectByName gets info of projects in lagoon that have matching metadata.
Expand Down Expand Up @@ -46,3 +47,9 @@ func RemoveProjectMetadataByKey(ctx context.Context, id int, key string, p Proje
project := schema.ProjectMetadata{}
return &project, p.RemoveProjectMetadataByKey(ctx, id, key, &project)
}

// GetSSHEndpointsByProject gets info of projects in lagoon that have matching metadata.
func GetSSHEndpointsByProject(ctx context.Context, name string, p Projects) (*schema.Project, error) {
project := schema.Project{}
return &project, p.SSHEndpointsByProject(ctx, name, &project)
}
1 change: 1 addition & 0 deletions internal/schema/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Environment struct {
Route string `json:"route,omitempty"`
Routes string `json:"routes,omitempty"`
Backups []Backup `json:"backups,omitempty"`
DeployTarget DeployTarget `json:"openshift,omitempty"`
// TODO use a unixtime type
Updated string `json:"updated,omitempty"`
Created string `json:"created,omitempty"`
Expand Down
8 changes: 8 additions & 0 deletions internal/schema/testdata/ciBranchPicky.golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,32 @@ projects:
deployType: branch
environmentType: production
name: master
openshift:
id: 1
openshiftProjectName: ci-branch-picky
- autoIdle: 1
deployBaseRef: develop
deployType: branch
environmentType: development
name: develop
openshift:
id: 1
openshiftProjectName: ci-branch-picky
- autoIdle: 1
deployBaseRef: stage
deployType: branch
environmentType: development
name: stage
openshift:
id: 1
openshiftProjectName: ci-branch-picky
- autoIdle: 1
deployBaseRef: banana
deployType: branch
environmentType: development
name: banana
openshift:
id: 1
openshiftProjectName: ci-branch-picky
gitUrl: ssh://git@192.168.42.1:2222/git/node.git
groups:
Expand Down
12 changes: 12 additions & 0 deletions internal/schema/testdata/ciBranchPicky.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@
"deployTitle": null,
"environmentType": "production",
"openshiftProjectName": "ci-branch-picky",
"openshift": {
"id": 1
},
"autoIdle": 1,
"envVariables": []
},
Expand All @@ -124,6 +127,9 @@
"deployTitle": null,
"environmentType": "development",
"openshiftProjectName": "ci-branch-picky",
"openshift": {
"id": 1
},
"autoIdle": 1,
"envVariables": []
},
Expand All @@ -135,6 +141,9 @@
"deployTitle": null,
"environmentType": "development",
"openshiftProjectName": "ci-branch-picky",
"openshift": {
"id": 1
},
"autoIdle": 1,
"envVariables": []
},
Expand All @@ -146,6 +155,9 @@
"deployTitle": null,
"environmentType": "development",
"openshiftProjectName": "ci-branch-picky",
"openshift": {
"id": 1
},
"autoIdle": 1,
"envVariables": []
}
Expand Down
2 changes: 2 additions & 0 deletions internal/schema/testdata/newNotifications.golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ projects:
deployType: branch
environmentType: production
name: master
openshift:
id: 1
openshiftProjectName: ci-local
gitUrl: ssh://git@192.168.42.1:2222/git/github.git
groups:
Expand Down
3 changes: 3 additions & 0 deletions internal/schema/testdata/newNotifications.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@
"deployTitle": null,
"environmentType": "production",
"openshiftProjectName": "ci-local",
"openshift": {
"id": 1
},
"autoIdle": 1,
"envVariables": []
}]
Expand Down
2 changes: 2 additions & 0 deletions internal/schema/testdata/noNewNotifications.golden.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ projects:
deployType: branch
environmentType: production
name: master
openshift:
id: 1
openshiftProjectName: ci-local
gitUrl: ssh://git@192.168.42.1:2222/git/github.git
groups:
Expand Down
3 changes: 3 additions & 0 deletions internal/schema/testdata/noNewNotifications.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@
"deployTitle": null,
"environmentType": "production",
"openshiftProjectName": "ci-local",
"openshift": {
"id": 1
},
"autoIdle": 1,
"envVariables": []
}]
Expand Down
Loading

0 comments on commit 85daffe

Please sign in to comment.