Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add user ssh queries #18

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
query (
$namespace: String!
){
environmentByNamespace:environmentByKubernetesNamespaceName(
kubernetesNamespaceName: $namespace
){
id
name
openshiftProjectName
openshift {
id
sshHost
sshPort
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
query ($fingerprint: String!) {
userBySshFingerprint(fingerprint: $fingerprint) {
id
email
firstName
lastName
comment
sshKeys {
id
name
keyType
keyValue
keyFingerprint
created
}
groupRoles {
name
role
}
}
}
21 changes: 21 additions & 0 deletions api/lagoon/client/_lgraphql/usergroups/userBySSHKey.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
query ($sshKey: String!) {
userBySshKey(sshKey: $sshKey) {
id
email
firstName
lastName
comment
sshKeys {
id
name
keyType
keyValue
keyFingerprint
created
}
groupRoles {
name
role
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query ($namespace: String!) {
userCanSshToEnvironment(kubernetesNamespaceName: $namespace) {
kubernetesNamespaceName
}
}
18 changes: 18 additions & 0 deletions api/lagoon/client/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,21 @@ func (c *Client) SetEnvironmentServices(
Response: result,
})
}

// SSHEndpointByNamespace queries the Lagoon API for an environment by its namespace
// and unmarshals the response into environment.
func (c *Client) SSHEndpointByNamespace(ctx context.Context, namespace string, environment *schema.Environment) error {
req, err := c.newRequest("_lgraphql/environments/sshEndpointByEnvironment.graphql",
map[string]interface{}{
"namespace": namespace,
})
if err != nil {
return err
}

return c.client.Run(ctx, req, &struct {
Response *schema.Environment `json:"environmentByNamespace"`
}{
Response: environment,
})
}
100 changes: 96 additions & 4 deletions api/lagoon/client/lgraphql/lgraphql.go

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions api/lagoon/client/usergroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,45 @@ func (c *Client) GetUserByEmail(
Response: user,
})
}

// UserCanSSHToEnvironment queries the Lagoon API as a user to check if the user has access to the environment, and
// unmarshals the response.
func (c *Client) UserCanSSHToEnvironment(
ctx context.Context, namespace string, environment *schema.Environment) error {

req, err := c.newRequest("_lgraphql/usergroups/userCanSSHToEnvironment.graphql",
map[string]string{"namespace": namespace})
if err != nil {
return err
}

return c.client.Run(ctx, req, &environment)
}

// UserBySSHKey queries the Lagoon API to find user by ssh key, and
// unmarshals the response.
func (c *Client) UserBySSHKey(
ctx context.Context, sshKey string, user *schema.User) error {

req, err := c.newRequest("_lgraphql/usergroups/userBySSHKey.graphql",
map[string]string{"sshKey": sshKey})
if err != nil {
return err
}

return c.client.Run(ctx, req, &user)
}

// UserBySSHFingerprint queries the Lagoon API to find user by ssh key fingerprint, and
// unmarshals the response.
func (c *Client) UserBySSHFingerprint(
ctx context.Context, fingerprint string, user *schema.User) error {

req, err := c.newRequest("_lgraphql/usergroups/userBySSHFingerprint.graphql",
map[string]string{"fingerprint": fingerprint})
if err != nil {
return err
}

return c.client.Run(ctx, req, &user)
}
7 changes: 7 additions & 0 deletions api/lagoon/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Environments interface {
SetEnvironmentServices(ctx context.Context, id uint, services []string, result *[]schema.EnvironmentService) error
EnvironmentByName(ctx context.Context, name string, project uint, result *schema.Environment) error
EnvironmentByNamespace(ctx context.Context, namespace string, result *schema.Environment) error
SSHEndpointByNamespace(ctx context.Context, namespace string, result *schema.Environment) error
}

// GetBackupsForEnvironmentByName gets backup info in lagoon for specific environment.
Expand Down Expand Up @@ -67,3 +68,9 @@ func GetEnvironmentByNamespace(ctx context.Context, namespace string, e Environm
environment := schema.Environment{}
return &environment, e.EnvironmentByNamespace(ctx, namespace, &environment)
}

// SSHEndpointByNamespace gets info of projects in lagoon that have matching metadata.
func SSHEndpointByNamespace(ctx context.Context, namespace string, e Environments) (*schema.Environment, error) {
environment := schema.Environment{}
return &environment, e.SSHEndpointByNamespace(ctx, namespace, &environment)
}
18 changes: 18 additions & 0 deletions api/lagoon/usergroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type UserGroups interface {
Me(ctx context.Context, user *schema.User) error
AllUsers(ctx context.Context, filter schema.AllUsersFilter, users *[]schema.User) error
GetUserByEmail(ctx context.Context, email string, user *schema.User) error
UserCanSSHToEnvironment(context.Context, string, *schema.Environment) error
UserBySSHKey(ctx context.Context, sshKey string, user *schema.User) error
UserBySSHFingerprint(ctx context.Context, fingerprint string, user *schema.User) error
}

// Me gets info on the current user of lagoon.
Expand Down Expand Up @@ -80,3 +83,18 @@ func GetUserByEmail(ctx context.Context, email string, ug UserGroups) (*schema.U
user := schema.User{}
return &user, ug.GetUserByEmail(ctx, email, &user)
}

func UserCanSSHToEnvironment(ctx context.Context, namespace string, ug UserGroups) (*schema.Environment, error) {
environment := schema.Environment{}
return &environment, ug.UserCanSSHToEnvironment(ctx, namespace, &environment)
}

func UserBySSHKey(ctx context.Context, sshKey string, ug UserGroups) (*schema.User, error) {
user := schema.User{}
return &user, ug.UserBySSHKey(ctx, sshKey, &user)
}

func UserBySSHFingerprint(ctx context.Context, fingerprint string, ug UserGroups) (*schema.User, error) {
user := schema.User{}
return &user, ug.UserBySSHFingerprint(ctx, fingerprint, &user)
}