Skip to content

Commit

Permalink
app_service - fix cors, ip_restriction and scm_ip_restriction
Browse files Browse the repository at this point in the history
… processing and minor bugfixes to flatten functions. (#20987)
  • Loading branch information
jackofallops authored Mar 23, 2023
1 parent d7dc52d commit 69847f2
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 201 deletions.
2 changes: 1 addition & 1 deletion internal/services/appservice/helpers/auto_heal.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func expandAutoHealSettingsWindows(autoHealSettings []AutoHealSettingWindows) *w

func flattenAutoHealSettingsWindows(autoHealRules *web.AutoHealRules) []AutoHealSettingWindows {
if autoHealRules == nil {
return nil
return []AutoHealSettingWindows{}
}

result := AutoHealSettingWindows{}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ func FlattenAppSettings(input web.StringDictionary) (map[string]string, *int, er

func flattenVirtualApplications(appVirtualApplications *[]web.VirtualApplication) []VirtualApplication {
if appVirtualApplications == nil || onlyDefaultVirtualApplication(*appVirtualApplications) {
return nil
return []VirtualApplication{}
}

var virtualApplications []VirtualApplication
Expand Down
43 changes: 2 additions & 41 deletions internal/services/appservice/helpers/function_app_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1961,6 +1961,7 @@ func FlattenSiteConfigLinuxFunctionApp(functionAppSiteConfig *web.SiteConfig) (*
AppCommandLine: utils.NormalizeNilableString(functionAppSiteConfig.AppCommandLine),
AppScaleLimit: int(utils.NormaliseNilableInt32(functionAppSiteConfig.FunctionAppScaleLimit)),
ContainerRegistryMSI: utils.NormalizeNilableString(functionAppSiteConfig.AcrUserManagedIdentityID),
Cors: FlattenCorsSettings(functionAppSiteConfig.Cors),
DetailedErrorLogging: utils.NormaliseNilableBool(functionAppSiteConfig.DetailedErrorLoggingEnabled),
HealthCheckPath: utils.NormalizeNilableString(functionAppSiteConfig.HealthCheckPath),
Http2Enabled: utils.NormaliseNilableBool(functionAppSiteConfig.HTTP20Enabled),
Expand Down Expand Up @@ -2004,26 +2005,6 @@ func FlattenSiteConfigLinuxFunctionApp(functionAppSiteConfig *web.SiteConfig) (*
result.DefaultDocuments = *v
}

if functionAppSiteConfig.Cors != nil {
corsEmpty := false
corsSettings := functionAppSiteConfig.Cors
cors := CorsSetting{}
if corsSettings.SupportCredentials != nil {
cors.SupportCredentials = *corsSettings.SupportCredentials
}

if corsSettings.AllowedOrigins != nil {
if len(*corsSettings.AllowedOrigins) > 0 {
cors.AllowedOrigins = *corsSettings.AllowedOrigins
} else if !cors.SupportCredentials {
corsEmpty = true
}
}
if !corsEmpty {
result.Cors = []CorsSetting{cors}
}
}

var appStack []ApplicationStackLinuxFunctionApp
if functionAppSiteConfig.LinuxFxVersion != nil {
decoded, err := DecodeFunctionAppLinuxFxVersion(*functionAppSiteConfig.LinuxFxVersion)
Expand All @@ -2046,6 +2027,7 @@ func FlattenSiteConfigWindowsFunctionApp(functionAppSiteConfig *web.SiteConfig)
AlwaysOn: utils.NormaliseNilableBool(functionAppSiteConfig.AlwaysOn),
AppCommandLine: utils.NormalizeNilableString(functionAppSiteConfig.AppCommandLine),
AppScaleLimit: int(utils.NormaliseNilableInt32(functionAppSiteConfig.FunctionAppScaleLimit)),
Cors: FlattenCorsSettings(functionAppSiteConfig.Cors),
DetailedErrorLogging: utils.NormaliseNilableBool(functionAppSiteConfig.DetailedErrorLoggingEnabled),
HealthCheckPath: utils.NormalizeNilableString(functionAppSiteConfig.HealthCheckPath),
Http2Enabled: utils.NormaliseNilableBool(functionAppSiteConfig.HTTP20Enabled),
Expand Down Expand Up @@ -2088,27 +2070,6 @@ func FlattenSiteConfigWindowsFunctionApp(functionAppSiteConfig *web.SiteConfig)
result.DefaultDocuments = *v
}

if functionAppSiteConfig.Cors != nil {
corsSettings := functionAppSiteConfig.Cors
corsEmpty := false
cors := CorsSetting{}
if corsSettings.SupportCredentials != nil {
cors.SupportCredentials = *corsSettings.SupportCredentials
}

if corsSettings.AllowedOrigins != nil {
if len(*corsSettings.AllowedOrigins) > 0 {
cors.AllowedOrigins = *corsSettings.AllowedOrigins
} else if !cors.SupportCredentials {
corsEmpty = true
}
}

if !corsEmpty {
result.Cors = []CorsSetting{cors}
}
}

powershellVersion := ""
if p := functionAppSiteConfig.PowerShellVersion; p != nil {
powershellVersion = *p
Expand Down
42 changes: 2 additions & 40 deletions internal/services/appservice/helpers/function_app_slot_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,7 @@ func FlattenSiteConfigWindowsFunctionAppSlot(functionAppSlotSiteConfig *web.Site
AppCommandLine: utils.NormalizeNilableString(functionAppSlotSiteConfig.AppCommandLine),
AppScaleLimit: int(utils.NormaliseNilableInt32(functionAppSlotSiteConfig.FunctionAppScaleLimit)),
AutoSwapSlotName: utils.NormalizeNilableString(functionAppSlotSiteConfig.AutoSwapSlotName),
Cors: FlattenCorsSettings(functionAppSlotSiteConfig.Cors),
DetailedErrorLogging: utils.NormaliseNilableBool(functionAppSlotSiteConfig.DetailedErrorLoggingEnabled),
HealthCheckPath: utils.NormalizeNilableString(functionAppSlotSiteConfig.HealthCheckPath),
Http2Enabled: utils.NormaliseNilableBool(functionAppSlotSiteConfig.HTTP20Enabled),
Expand Down Expand Up @@ -888,26 +889,6 @@ func FlattenSiteConfigWindowsFunctionAppSlot(functionAppSlotSiteConfig *web.Site
result.DefaultDocuments = *v
}

if functionAppSlotSiteConfig.Cors != nil {
corsEmpty := false
corsSettings := functionAppSlotSiteConfig.Cors
cors := CorsSetting{}
if corsSettings.SupportCredentials != nil {
cors.SupportCredentials = *corsSettings.SupportCredentials
}

if corsSettings.AllowedOrigins != nil {
if len(*corsSettings.AllowedOrigins) > 0 {
cors.AllowedOrigins = *corsSettings.AllowedOrigins
} else if !cors.SupportCredentials {
corsEmpty = true
}
}
if !corsEmpty {
result.Cors = []CorsSetting{cors}
}
}

powershellVersion := ""
if p := functionAppSlotSiteConfig.PowerShellVersion; p != nil {
powershellVersion = *p
Expand Down Expand Up @@ -1199,6 +1180,7 @@ func FlattenSiteConfigLinuxFunctionAppSlot(functionAppSlotSiteConfig *web.SiteCo
AppScaleLimit: int(utils.NormaliseNilableInt32(functionAppSlotSiteConfig.FunctionAppScaleLimit)),
AutoSwapSlotName: utils.NormalizeNilableString(functionAppSlotSiteConfig.AutoSwapSlotName),
ContainerRegistryMSI: utils.NormalizeNilableString(functionAppSlotSiteConfig.AcrUserManagedIdentityID),
Cors: FlattenCorsSettings(functionAppSlotSiteConfig.Cors),
DetailedErrorLogging: utils.NormaliseNilableBool(functionAppSlotSiteConfig.DetailedErrorLoggingEnabled),
HealthCheckPath: utils.NormalizeNilableString(functionAppSlotSiteConfig.HealthCheckPath),
Http2Enabled: utils.NormaliseNilableBool(functionAppSlotSiteConfig.HTTP20Enabled),
Expand Down Expand Up @@ -1242,26 +1224,6 @@ func FlattenSiteConfigLinuxFunctionAppSlot(functionAppSlotSiteConfig *web.SiteCo
result.DefaultDocuments = *v
}

if functionAppSlotSiteConfig.Cors != nil {
corsSettings := functionAppSlotSiteConfig.Cors
corsEmpty := false
cors := CorsSetting{}
if corsSettings.SupportCredentials != nil {
cors.SupportCredentials = *corsSettings.SupportCredentials
}

if corsSettings.AllowedOrigins != nil {
if len(*corsSettings.AllowedOrigins) > 0 {
cors.AllowedOrigins = *corsSettings.AllowedOrigins
} else if !cors.SupportCredentials {
corsEmpty = true
}
}
if !corsEmpty {
result.Cors = []CorsSetting{cors}
}
}

var appStack []ApplicationStackLinuxFunctionApp
if functionAppSlotSiteConfig.LinuxFxVersion != nil {
decoded, err := DecodeFunctionAppLinuxFxVersion(*functionAppSlotSiteConfig.LinuxFxVersion)
Expand Down
25 changes: 3 additions & 22 deletions internal/services/appservice/helpers/linux_web_app_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,7 @@ func ExpandSiteConfigLinux(siteConfig []SiteConfigLinux, existing *web.SiteConfi

func FlattenSiteConfigLinux(appSiteConfig *web.SiteConfig, healthCheckCount *int) []SiteConfigLinux {
if appSiteConfig == nil {
return nil
return []SiteConfigLinux{}
}

siteConfig := SiteConfigLinux{
Expand Down Expand Up @@ -930,6 +930,7 @@ func FlattenSiteConfigLinux(appSiteConfig *web.SiteConfig, healthCheckCount *int
UseManagedIdentityACR: pointer.From(appSiteConfig.AcrUseManagedIdentityCreds),
WebSockets: pointer.From(appSiteConfig.WebSocketsEnabled),
VnetRouteAllEnabled: pointer.From(appSiteConfig.VnetRouteAllEnabled),
Cors: FlattenCorsSettings(appSiteConfig.Cors),
}

if appSiteConfig.APIManagementConfig != nil && appSiteConfig.APIManagementConfig.ID != nil {
Expand All @@ -952,26 +953,6 @@ func FlattenSiteConfigLinux(appSiteConfig *web.SiteConfig, healthCheckCount *int
siteConfig.ApplicationStack = []ApplicationStackLinux{linuxAppStack}
}

if appSiteConfig.Cors != nil {
corsEmpty := false
corsSettings := appSiteConfig.Cors
cors := CorsSetting{}
if corsSettings.SupportCredentials != nil {
cors.SupportCredentials = *corsSettings.SupportCredentials
}

if corsSettings.AllowedOrigins != nil {
if len(*corsSettings.AllowedOrigins) > 0 {
cors.AllowedOrigins = *corsSettings.AllowedOrigins
} else if !cors.SupportCredentials {
corsEmpty = true
}
}
if !corsEmpty {
siteConfig.Cors = []CorsSetting{cors}
}
}

return []SiteConfigLinux{siteConfig}
}

Expand Down Expand Up @@ -1047,7 +1028,7 @@ func expandAutoHealSettingsLinux(autoHealSettings []AutoHealSettingLinux) *web.A

func flattenAutoHealSettingsLinux(autoHealRules *web.AutoHealRules) []AutoHealSettingLinux {
if autoHealRules == nil {
return nil
return []AutoHealSettingLinux{}
}

result := AutoHealSettingLinux{}
Expand Down
62 changes: 35 additions & 27 deletions internal/services/appservice/helpers/shared_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ func (v IpRestriction) Validate() error {

func IpRestrictionSchema() *pluginsdk.Schema {
return &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Optional: true,
Computed: true,
ConfigMode: pluginsdk.SchemaConfigModeAttr,
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"ip_address": {
Expand Down Expand Up @@ -273,7 +271,8 @@ func CorsSettingsSchema() *pluginsdk.Schema {
Schema: map[string]*pluginsdk.Schema{
"allowed_origins": {
Type: pluginsdk.TypeSet,
Required: true,
Optional: true,
//MinItems: 1,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
Expand Down Expand Up @@ -316,6 +315,34 @@ func CorsSettingsSchemaComputed() *pluginsdk.Schema {
}
}

func FlattenCorsSettings(input *web.CorsSettings) []CorsSetting {
if input == nil {
return []CorsSetting{}
}

cors := *input
if len(pointer.From(cors.AllowedOrigins)) == 0 && !pointer.From(cors.SupportCredentials) {
return []CorsSetting{}
}

return []CorsSetting{{
SupportCredentials: pointer.From(cors.SupportCredentials),
AllowedOrigins: pointer.From(cors.AllowedOrigins),
}}
}

func ExpandCorsSettings(input []CorsSetting) *web.CorsSettings {
if len(input) != 1 {
return &web.CorsSettings{}
}
cors := input[0]

return &web.CorsSettings{
AllowedOrigins: pointer.To(cors.AllowedOrigins),
SupportCredentials: pointer.To(cors.SupportCredentials),
}
}

type SourceControl struct {
RepoURL string `tfschema:"repo_url"`
Branch string `tfschema:"branch"`
Expand Down Expand Up @@ -1095,7 +1122,7 @@ func GithubAuthSettingsSchemaComputed() *pluginsdk.Schema {
}

func ExpandIpRestrictions(restrictions []IpRestriction) (*[]web.IPSecurityRestriction, error) {
var expanded []web.IPSecurityRestriction
expanded := make([]web.IPSecurityRestriction, 0)
if len(restrictions) == 0 {
return &expanded, nil
}
Expand Down Expand Up @@ -1159,25 +1186,6 @@ func expandIpRestrictionHeaders(headers []IpRestrictionHeaders) map[string][]str
return result
}

func ExpandCorsSettings(input []CorsSetting) *web.CorsSettings {
if len(input) == 0 {
allowedOrigins := make([]string, 0)
return &web.CorsSettings{
AllowedOrigins: &allowedOrigins,
SupportCredentials: pointer.To(false),
}
}
var result web.CorsSettings
for _, v := range input {
if v.SupportCredentials {
result.SupportCredentials = utils.Bool(v.SupportCredentials)
}

result.AllowedOrigins = &v.AllowedOrigins
}
return &result
}

func ExpandAuthSettings(auth []AuthSettings) *web.SiteAuthSettings {
result := &web.SiteAuthSettings{}
if len(auth) == 0 {
Expand Down Expand Up @@ -1444,7 +1452,7 @@ func FlattenAuthSettings(auth web.SiteAuthSettings) []AuthSettings {

func FlattenIpRestrictions(ipRestrictionsList *[]web.IPSecurityRestriction) []IpRestriction {
if ipRestrictionsList == nil {
return nil
return []IpRestriction{}
}

var ipRestrictions []IpRestriction
Expand Down Expand Up @@ -1489,7 +1497,7 @@ func FlattenIpRestrictions(ipRestrictionsList *[]web.IPSecurityRestriction) []Ip

func flattenIpRestrictionHeaders(headers map[string][]string) []IpRestrictionHeaders {
if len(headers) == 0 {
return nil
return []IpRestrictionHeaders{}
}
ipRestrictionHeader := IpRestrictionHeaders{}
if xForwardFor, ok := headers["x-forwarded-for"]; ok {
Expand Down
43 changes: 2 additions & 41 deletions internal/services/appservice/helpers/web_app_slot_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ func FlattenSiteConfigLinuxWebAppSlot(appSiteSlotConfig *web.SiteConfig, healthC
AutoHealSettings: flattenAutoHealSettingsLinux(appSiteSlotConfig.AutoHealRules),
AutoSwapSlotName: pointer.From(appSiteSlotConfig.AutoSwapSlotName),
ContainerRegistryMSI: pointer.From(appSiteSlotConfig.AcrUserManagedIdentityID),
Cors: FlattenCorsSettings(appSiteSlotConfig.Cors),
DetailedErrorLogging: pointer.From(appSiteSlotConfig.DetailedErrorLoggingEnabled),
Http2Enabled: pointer.From(appSiteSlotConfig.HTTP20Enabled),
IpRestriction: FlattenIpRestrictions(appSiteSlotConfig.IPSecurityRestrictions),
Expand Down Expand Up @@ -770,27 +771,6 @@ func FlattenSiteConfigLinuxWebAppSlot(appSiteSlotConfig *web.SiteConfig, healthC
siteConfig.ApplicationStack = []ApplicationStackLinux{linuxAppStack}
}

if appSiteSlotConfig.Cors != nil {
corsEmpty := false
corsSettings := appSiteSlotConfig.Cors
cors := CorsSetting{}
if corsSettings.SupportCredentials != nil {
cors.SupportCredentials = *corsSettings.SupportCredentials
}

if corsSettings.AllowedOrigins != nil {
if len(*corsSettings.AllowedOrigins) > 0 {
cors.AllowedOrigins = *corsSettings.AllowedOrigins
} else if !cors.SupportCredentials {
corsEmpty = true
}
}

if !corsEmpty {
siteConfig.Cors = []CorsSetting{cors}
}
}

return []SiteConfigLinuxWebAppSlot{siteConfig}
}

Expand Down Expand Up @@ -1033,6 +1013,7 @@ func FlattenSiteConfigWindowsAppSlot(appSiteSlotConfig *web.SiteConfig, currentS
AutoHealSettings: flattenAutoHealSettingsWindows(appSiteSlotConfig.AutoHealRules),
AutoSwapSlotName: pointer.From(appSiteSlotConfig.AutoSwapSlotName),
ContainerRegistryUserMSI: pointer.From(appSiteSlotConfig.AcrUserManagedIdentityID),
Cors: FlattenCorsSettings(appSiteSlotConfig.Cors),
DetailedErrorLogging: pointer.From(appSiteSlotConfig.DetailedErrorLoggingEnabled),
FtpsState: string(appSiteSlotConfig.FtpsState),
HealthCheckPath: pointer.From(appSiteSlotConfig.HealthCheckPath),
Expand Down Expand Up @@ -1112,25 +1093,5 @@ func FlattenSiteConfigWindowsAppSlot(appSiteSlotConfig *web.SiteConfig, currentS

siteConfig.ApplicationStack = []ApplicationStackWindows{winAppStack}

if appSiteSlotConfig.Cors != nil {
corsEmpty := false
corsSettings := appSiteSlotConfig.Cors
cors := CorsSetting{}
if corsSettings.SupportCredentials != nil {
cors.SupportCredentials = *corsSettings.SupportCredentials
}

if corsSettings.AllowedOrigins != nil {
if len(*corsSettings.AllowedOrigins) > 0 {
cors.AllowedOrigins = *corsSettings.AllowedOrigins
} else if !cors.SupportCredentials {
corsEmpty = true
}
}
if !corsEmpty {
siteConfig.Cors = []CorsSetting{cors}
}
}

return []SiteConfigWindowsWebAppSlot{siteConfig}
}
Loading

0 comments on commit 69847f2

Please sign in to comment.