Skip to content
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
5 changes: 3 additions & 2 deletions sysdig/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ const (
SchemaCloudLogsMetadata = "cloud_logs_metadata"
SchemaEnabled = "enabled"
SchemaComponents = "components"
SchemaComponent = "component"
SchemaId = "id"
SchemaCloudProviderId = "cloud_provider_id"
SchemaCloudProviderType = "cloud_provider_type"
SchemaCloudProviderId = "provider_id"
SchemaCloudProviderType = "provider_type"
SchemaFeature = "feature"
)
83 changes: 54 additions & 29 deletions sysdig/resource_sysdig_secure_cloud_auth_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func resourceSysdigSecureCloudauthAccount() *schema.Resource {
Optional: true,
Elem: accountFeatures,
},
SchemaComponents: {
SchemaComponent: {
Type: schema.TypeList,
Optional: true,
Elem: accountComponents,
Expand Down Expand Up @@ -309,7 +309,7 @@ cloudauthAccountFromResourceData() function
func constructAccountComponents(accountComponents []*cloudauth.AccountComponent, data *schema.ResourceData) []*cloudauth.AccountComponent {
provider := data.Get(SchemaCloudProviderType).(string)

for _, rc := range data.Get(SchemaComponents).([]interface{}) {
for _, rc := range data.Get(SchemaComponent).([]interface{}) {
resourceComponent := rc.(map[string]interface{})
component := &cloudauth.AccountComponent{}

Expand Down Expand Up @@ -343,19 +343,26 @@ func constructAccountComponents(accountComponents []*cloudauth.AccountComponent,
}
case SchemaServicePrincipalMetadata:
// TODO: Make it more generic than just for GCP
service_principal_private_key := getServicePrincipalKeyObject(value.(string))
component.Metadata = &cloudauth.AccountComponent_ServicePrincipalMetadata{
ServicePrincipalMetadata: &cloudauth.ServicePrincipalMetadata{
Provider: &cloudauth.ServicePrincipalMetadata_Gcp{
Gcp: &cloudauth.ServicePrincipalMetadata_GCP{
Key: &cloudauth.ServicePrincipalMetadata_GCP_Key{
ProjectId: data.Get(SchemaCloudProviderId).(string),
PrivateKeyId: service_principal_private_key["private_key_id"],
PrivateKey: service_principal_private_key["private_key"],
servicePrincipalMetadata := parseMetadataJson(value.(string))
if provider == cloudauth.Provider_PROVIDER_GCP.String() {
encodedServicePrincipalKey, ok := servicePrincipalMetadata["gcp"].(map[string]interface{})["key"].(string)
if !ok {
fmt.Printf("Component metadata for provider %s is invalid and not as expected", provider)
}
servicePrincipalKey := getGcpServicePrincipalKey(encodedServicePrincipalKey)
component.Metadata = &cloudauth.AccountComponent_ServicePrincipalMetadata{
ServicePrincipalMetadata: &cloudauth.ServicePrincipalMetadata{
Provider: &cloudauth.ServicePrincipalMetadata_Gcp{
Gcp: &cloudauth.ServicePrincipalMetadata_GCP{
Key: &cloudauth.ServicePrincipalMetadata_GCP_Key{
ProjectId: data.Get(SchemaCloudProviderId).(string),
PrivateKeyId: servicePrincipalKey["private_key_id"],
PrivateKey: servicePrincipalKey["private_key"],
},
},
},
},
},
}
}
case SchemaWebhookDatasourceMetadata:
component.Metadata = &cloudauth.AccountComponent_WebhookDatasourceMetadata{
Expand All @@ -379,6 +386,40 @@ func constructAccountComponents(accountComponents []*cloudauth.AccountComponent,
return accountComponents
}

/*
This helper function parses the provided component metadata in opaque Json string format into a map
*/
func parseMetadataJson(value string) map[string]interface{} {
var metadataJSON map[string]interface{}
err := json.Unmarshal([]byte(value), &metadataJSON)
if err != nil {
fmt.Printf("Failed to parse component metadata: %v", err)
return nil
}

return metadataJSON
}

/*
This helper function decodes the base64 encoded Service Principal Key returned by GCP
and parses it from Json format into a map
*/
func getGcpServicePrincipalKey(key string) map[string]string {
bytes, err := b64.StdEncoding.DecodeString(key)
if err != nil {
fmt.Printf("Failed to decode service principal key: %v", err)
return nil
}
var privateKeyJSON map[string]string
err = json.Unmarshal(bytes, &privateKeyJSON)
if err != nil {
fmt.Printf("Failed to parse service principal key: %v", err)
return nil
}

return privateKeyJSON
}

func cloudauthAccountFromResourceData(data *schema.ResourceData) *v2.CloudauthAccountSecure {
accountComponents := constructAccountComponents([]*cloudauth.AccountComponent{}, data)

Expand Down Expand Up @@ -477,26 +518,10 @@ func cloudauthAccountToResourceData(data *schema.ResourceData, cloudAccount *v2.
})
}

err = data.Set(SchemaComponents, components)
err = data.Set(SchemaComponent, components)
if err != nil {
return err
}

return nil
}

func getServicePrincipalKeyObject(value string) map[string]string {
bytes, err := b64.StdEncoding.DecodeString(value)
if err != nil {
fmt.Printf("Failed to decode service principal key: %v", err)
return nil
}
var privateKeyJSON map[string]string
err = json.Unmarshal(bytes, &privateKeyJSON)
if err != nil {
fmt.Printf("Failed to parse service principal key: %v", err)
return nil
}

return privateKeyJSON
}