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

DUPLO-13529 Detect token invalidation from user logout #23

Merged
merged 7 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 13 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@
"--interactive"
],
},
{
"name": "Duplo",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/duplo-jit/main.go",
"args": [
"duplo",
"--host",
"https://test20.duplocloud.net",
"--interactive"
],
},
{
"name": "K8s Plan",
"type": "go",
Expand Down
4 changes: 2 additions & 2 deletions cmd/duplo-aws-credential-process/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func main() {
// Otherwise, get the credentials from Duplo.
if creds == nil {
client := mustDuploClient(*host, *token, *interactive, true)
result, err := client.AdminGetJITAwsCredentials()
result, err := client.AdminGetJitAwsCredentials()
internal.DieIf(err, "failed to get credentials")
creds = internal.ConvertAwsCreds(result)
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func main() {
}

// Tenant: Get the JIT AWS credentials
result, err := client.TenantGetJITAwsCredentials(*tenantID)
result, err := client.TenantGetJitAwsCredentials(*tenantID)
internal.DieIf(err, "failed to get credentials")
creds = internal.ConvertAwsCreds(result)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/duplo-jit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func main() {
// Otherwise, get the credentials from Duplo.
if creds == nil {
client, _ := internal.MustDuploClient(*host, *token, *interactive, true)
result, err := client.AdminGetJITAwsCredentials()
result, err := client.AdminGetJitAwsCredentials()
internal.DieIf(err, "failed to get credentials")
creds = internal.ConvertAwsCreds(result)
}
Expand Down Expand Up @@ -148,7 +148,7 @@ func main() {
// Otherwise, get the credentials from Duplo.
if creds == nil {
// Tenant: Get the JIT AWS credentials
result, err := client.TenantGetJITAwsCredentials(*tenantID)
result, err := client.TenantGetJitAwsCredentials(*tenantID)
internal.DieIf(err, "failed to get credentials")
creds = internal.ConvertAwsCreds(result)
}
Expand Down
29 changes: 26 additions & 3 deletions duplocloud/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ type DuploSystemFeatures struct {
TenantNameMaxLength int `json:"TenantNameMaxLength"`
}

// DuploInfrastructureConfig represents an infrastructure configuration
type DuploInfrastructure struct {
Name string `json:"Name,omitempty"`
Region string `json:"Region,omitempty"`
EnableK8Cluster bool `json:"EnableK8Cluster,omitempty"`
EnableECSCluster bool `json:"EnableECSCluster,omitempty"`
EnableContainerInsights bool `json:"EnableContainerInsights,omitempty"`
ProvisioningStatus string `json:"ProvisioningStatus,omitempty"`
}

// DuploPlanK8ClusterConfig represents a k8s system configuration
type DuploPlanK8ClusterConfig struct {
Name string `json:"Name,omitempty"`
Expand Down Expand Up @@ -94,15 +104,15 @@ func (c *Client) AdminGetK8sJitAccess(plan string) (*DuploPlanK8ClusterConfig, C
}

// AdminGetJITAwsCredentials retrieves just-in-time admin AWS credentials via the Duplo API.
func (c *Client) AdminGetJITAwsCredentials() (*AwsJitCredentials, ClientError) {
func (c *Client) AdminGetJitAwsCredentials() (*AwsJitCredentials, ClientError) {
return c.AdminAwsGetJitAccess("admin")
}

// TenantGetJITAwsCredentials retrieves just-in-time AWS credentials for a tenant via the Duplo API.
func (c *Client) TenantGetJITAwsCredentials(tenantID string) (*AwsJitCredentials, ClientError) {
func (c *Client) TenantGetJitAwsCredentials(tenantID string) (*AwsJitCredentials, ClientError) {
creds := AwsJitCredentials{}
err := c.getAPI(
fmt.Sprintf("TenantGetAwsCredentials(%s)", tenantID),
fmt.Sprintf("TenantGetJitAwsCredentials(%s)", tenantID),
fmt.Sprintf("subscriptions/%s/GetAwsConsoleTokenUrl", tenantID),
&creds,
)
Expand Down Expand Up @@ -136,6 +146,19 @@ func (c *Client) ListTenantsForUser() (*[]UserTenant, ClientError) {
return &list, nil
}

func (c *Client) AdminGetInfrastructure(infraName string) (*DuploInfrastructure, ClientError) {
config := DuploInfrastructure{}
err := c.getAPI(
fmt.Sprintf("AdminGetInfrastructure(%s)", infraName),
fmt.Sprintf("v3/admin/infrastructure/%s", infraName),
&config,
)
if err != nil {
return nil, err
}
return &config, nil
}

// GetTenantByNameForUser retrieves a single tenant by name for the current user via the Duplo API.
func (c *Client) GetTenantByNameForUser(name string) (*UserTenant, ClientError) {
// Get all tenants.
Expand Down
9 changes: 8 additions & 1 deletion internal/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func CacheGetDuploOutput(cacheKey string, host string) (creds *DuploCredsOutput)

// Check credentials for expiry - by trying to retrieve system features
if creds != nil {
// Retrieve system features. This also validates the creds.
// Retrieve system features.
client, err := duplocloud.NewClient(host, creds.DuploToken)
if err == nil {
var features *duplocloud.DuploSystemFeatures
Expand All @@ -158,6 +158,13 @@ func CacheGetDuploOutput(cacheKey string, host string) (creds *DuploCredsOutput)
}
}

// Validate creds by executing ping
if creds != nil {
if err := PingDuploCreds(creds, host); err != nil {
creds = nil
}
}

// Clear the cache if the creds expired.
if creds == nil {
cacheRemoveFile(cacheKey, file)
Expand Down
14 changes: 14 additions & 0 deletions internal/duplo.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,17 @@ func OutputDuploCreds(creds *DuploCredsOutput) {
os.Stdout.Write(json)
os.Stdout.WriteString("\n")
}

func PingDuploCreds(creds *DuploCredsOutput, host string) error {
client, err := duplocloud.NewClient(host, creds.DuploToken)
if err != nil {
return err
}

_, cerr := client.AdminGetInfrastructure("default")
duplodavid marked this conversation as resolved.
Show resolved Hide resolved
if cerr != nil && cerr.Status() != 404 {
return cerr
}

return nil
}