Skip to content

Commit 2eebe1a

Browse files
authored
Merge pull request #4843 from GreenStage/egomes/fix-saas-app-nllptr
2 parents 7e5f90c + 915a783 commit 2eebe1a

File tree

2 files changed

+66
-55
lines changed

2 files changed

+66
-55
lines changed

.changelog/4843.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/cloudflare_access_application: Fix access application saas apps attributes crashing provider when no changes were made to those
3+
```

internal/sdkv2provider/schema_cloudflare_access_application.go

+63-55
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import (
1212
"github.com/pkg/errors"
1313
)
1414

15+
const (
16+
saasAuthTypeOIDC = "oidc"
17+
saasAuthTypeSAML = "saml"
18+
)
19+
1520
func resourceCloudflareAccessApplicationSchema() map[string]*schema.Schema {
1621
return map[string]*schema.Schema{
1722
consts.AccountIDSchemaKey: {
@@ -959,67 +964,70 @@ func convertOIDCClaimSchemaToStruct(data map[string]interface{}) cloudflare.OIDC
959964
return cfg
960965
}
961966

962-
func convertSaasSchemaToStruct(d *schema.ResourceData) *cloudflare.SaasApplication {
963-
SaasConfig := cloudflare.SaasApplication{}
964-
if _, ok := d.GetOk("saas_app"); ok {
965-
authType := "saml"
966-
if rawAuthType, ok := d.GetOk("saas_app.0.auth_type"); ok {
967-
authType = rawAuthType.(string)
967+
func convertSaasOIDCSchemaToStruct(d *schema.ResourceData) *cloudflare.SaasApplication {
968+
var oidcConfig cloudflare.SaasApplication
969+
oidcConfig.AuthType = saasAuthTypeOIDC
970+
oidcConfig.ClientID = d.Get("saas_app.0.client_id").(string)
971+
oidcConfig.AppLauncherURL = d.Get("saas_app.0.app_launcher_url").(string)
972+
oidcConfig.RedirectURIs = expandInterfaceToStringList(d.Get("saas_app.0.redirect_uris").(*schema.Set).List())
973+
oidcConfig.GrantTypes = expandInterfaceToStringList(d.Get("saas_app.0.grant_types").(*schema.Set).List())
974+
oidcConfig.Scopes = expandInterfaceToStringList(d.Get("saas_app.0.scopes").(*schema.Set).List())
975+
oidcConfig.GroupFilterRegex = d.Get("saas_app.0.group_filter_regex").(string)
976+
oidcConfig.AccessTokenLifetime = d.Get("saas_app.0.access_token_lifetime").(string)
977+
oidcConfig.AllowPKCEWithoutClientSecret = cloudflare.BoolPtr(d.Get("saas_app.0.allow_pkce_without_client_secret").(bool))
978+
if _, ok := d.GetOk("saas_app.0.refresh_token_options"); ok {
979+
oidcConfig.RefreshTokenOptions = &cloudflare.RefreshTokenOptions{
980+
Lifetime: d.Get("saas_app.0.refresh_token_options.0.lifetime").(string),
968981
}
969-
SaasConfig.AuthType = authType
970-
if authType == "oidc" {
971-
SaasConfig.ClientID = d.Get("saas_app.0.client_id").(string)
972-
SaasConfig.AppLauncherURL = d.Get("saas_app.0.app_launcher_url").(string)
973-
SaasConfig.RedirectURIs = expandInterfaceToStringList(d.Get("saas_app.0.redirect_uris").(*schema.Set).List())
974-
SaasConfig.GrantTypes = expandInterfaceToStringList(d.Get("saas_app.0.grant_types").(*schema.Set).List())
975-
SaasConfig.Scopes = expandInterfaceToStringList(d.Get("saas_app.0.scopes").(*schema.Set).List())
976-
SaasConfig.GroupFilterRegex = d.Get("saas_app.0.group_filter_regex").(string)
977-
SaasConfig.AccessTokenLifetime = d.Get("saas_app.0.access_token_lifetime").(string)
978-
SaasConfig.AllowPKCEWithoutClientSecret = cloudflare.BoolPtr(d.Get("saas_app.0.allow_pkce_without_client_secret").(bool))
979-
if _, ok := d.GetOk("saas_app.0.refresh_token_options"); ok {
980-
SaasConfig.RefreshTokenOptions = &cloudflare.RefreshTokenOptions{
981-
Lifetime: d.Get("saas_app.0.refresh_token_options.0.lifetime").(string),
982-
}
983-
}
984-
985-
if d.HasChange("saas_app.0.custom_claim") {
986-
SaasConfig.CustomClaims = &[]cloudflare.OIDCClaimConfig{}
987-
}
982+
}
988983

989-
customClaims, _ := d.Get("saas_app.0.custom_claim").([]interface{})
990-
for _, customClaims := range customClaims {
991-
claimAsMap := customClaims.(map[string]interface{})
992-
claim := convertOIDCClaimSchemaToStruct(claimAsMap)
993-
*SaasConfig.CustomClaims = append(*SaasConfig.CustomClaims, claim)
994-
}
984+
customClaims, _ := d.Get("saas_app.0.custom_claim").([]interface{})
985+
if len(customClaims) != 0 {
986+
oidcConfig.CustomClaims = &[]cloudflare.OIDCClaimConfig{}
987+
for _, customClaims := range customClaims {
988+
claimAsMap := customClaims.(map[string]interface{})
989+
claim := convertOIDCClaimSchemaToStruct(claimAsMap)
990+
*oidcConfig.CustomClaims = append(*oidcConfig.CustomClaims, claim)
991+
}
992+
}
995993

996-
if _, ok := d.GetOk("saas_app.0.hybrid_and_implicit_options"); ok {
997-
SaasConfig.HybridAndImplicitOptions = &cloudflare.AccessApplicationHybridAndImplicitOptions{
998-
ReturnAccessTokenFromAuthorizationEndpoint: cloudflare.BoolPtr(d.Get("saas_app.0.hybrid_and_implicit_options.0.return_access_token_from_authorization_endpoint").(bool)),
999-
ReturnIDTokenFromAuthorizationEndpoint: cloudflare.BoolPtr(d.Get("saas_app.0.hybrid_and_implicit_options.0.return_id_token_from_authorization_endpoint").(bool)),
1000-
}
1001-
}
1002-
} else {
1003-
SaasConfig.SPEntityID = d.Get("saas_app.0.sp_entity_id").(string)
1004-
SaasConfig.ConsumerServiceUrl = d.Get("saas_app.0.consumer_service_url").(string)
1005-
SaasConfig.NameIDFormat = d.Get("saas_app.0.name_id_format").(string)
1006-
SaasConfig.DefaultRelayState = d.Get("saas_app.0.default_relay_state").(string)
1007-
SaasConfig.NameIDTransformJsonata = d.Get("saas_app.0.name_id_transform_jsonata").(string)
1008-
SaasConfig.SamlAttributeTransformJsonata = d.Get("saas_app.0.saml_attribute_transform_jsonata").(string)
1009-
1010-
if d.HasChanges("saas_app.0.custom_attribute") {
1011-
SaasConfig.CustomAttributes = &[]cloudflare.SAMLAttributeConfig{}
1012-
}
994+
if _, ok := d.GetOk("saas_app.0.hybrid_and_implicit_options"); ok {
995+
oidcConfig.HybridAndImplicitOptions = &cloudflare.AccessApplicationHybridAndImplicitOptions{
996+
ReturnAccessTokenFromAuthorizationEndpoint: cloudflare.BoolPtr(d.Get("saas_app.0.hybrid_and_implicit_options.0.return_access_token_from_authorization_endpoint").(bool)),
997+
ReturnIDTokenFromAuthorizationEndpoint: cloudflare.BoolPtr(d.Get("saas_app.0.hybrid_and_implicit_options.0.return_id_token_from_authorization_endpoint").(bool)),
998+
}
999+
}
1000+
return &oidcConfig
1001+
}
10131002

1014-
customAttributes, _ := d.Get("saas_app.0.custom_attribute").([]interface{})
1015-
for _, customAttributes := range customAttributes {
1016-
attributeAsMap := customAttributes.(map[string]interface{})
1017-
attribute := convertSAMLAttributeSchemaToStruct(attributeAsMap)
1018-
*SaasConfig.CustomAttributes = append(*SaasConfig.CustomAttributes, attribute)
1019-
}
1003+
func convertSaasSAMLSchemaToStruct(d *schema.ResourceData) *cloudflare.SaasApplication {
1004+
var samlConfig cloudflare.SaasApplication
1005+
samlConfig.AuthType = saasAuthTypeSAML
1006+
samlConfig.SPEntityID = d.Get("saas_app.0.sp_entity_id").(string)
1007+
samlConfig.ConsumerServiceUrl = d.Get("saas_app.0.consumer_service_url").(string)
1008+
samlConfig.NameIDFormat = d.Get("saas_app.0.name_id_format").(string)
1009+
samlConfig.DefaultRelayState = d.Get("saas_app.0.default_relay_state").(string)
1010+
samlConfig.NameIDTransformJsonata = d.Get("saas_app.0.name_id_transform_jsonata").(string)
1011+
samlConfig.SamlAttributeTransformJsonata = d.Get("saas_app.0.saml_attribute_transform_jsonata").(string)
1012+
1013+
customAttributes, _ := d.Get("saas_app.0.custom_attribute").([]interface{})
1014+
if len(customAttributes) != 0 {
1015+
samlConfig.CustomAttributes = &[]cloudflare.SAMLAttributeConfig{}
1016+
for _, customAttributes := range customAttributes {
1017+
attributeAsMap := customAttributes.(map[string]interface{})
1018+
attribute := convertSAMLAttributeSchemaToStruct(attributeAsMap)
1019+
*samlConfig.CustomAttributes = append(*samlConfig.CustomAttributes, attribute)
10201020
}
10211021
}
1022-
return &SaasConfig
1022+
return &samlConfig
1023+
}
1024+
1025+
func convertSaasSchemaToStruct(d *schema.ResourceData) *cloudflare.SaasApplication {
1026+
if authType, _ := d.GetOk("saas_app.0.auth_type"); authType == "oidc" {
1027+
return convertSaasOIDCSchemaToStruct(d)
1028+
} else {
1029+
return convertSaasSAMLSchemaToStruct(d)
1030+
}
10231031
}
10241032

10251033
func convertDestinationsToStruct(destinationPayloads []interface{}) ([]cloudflare.AccessDestination, error) {

0 commit comments

Comments
 (0)