-
Notifications
You must be signed in to change notification settings - Fork 376
Ensure security defenses settings are set on setRealmData realm (#1257) #1279
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -475,6 +475,7 @@ func resourceKeycloakRealm() *schema.Resource { | |
| "security_defenses": { | ||
| Type: schema.TypeList, | ||
| Optional: true, | ||
| Computed: true, | ||
| MaxItems: 1, | ||
| Elem: &schema.Resource{ | ||
| Schema: map[string]*schema.Schema{ | ||
|
|
@@ -998,7 +999,7 @@ func getRealmFromData(data *schema.ResourceData, keycloakVersion *version.Versio | |
| if len(headersConfig) == 1 { | ||
| headerSettings := headersConfig[0].(map[string]interface{}) | ||
|
|
||
| realm.BrowserSecurityHeaders = keycloak.BrowserSecurityHeaders{ | ||
| realm.BrowserSecurityHeaders = &keycloak.BrowserSecurityHeaders{ | ||
| ContentSecurityPolicy: headerSettings["content_security_policy"].(string), | ||
| ContentSecurityPolicyReportOnly: headerSettings["content_security_policy_report_only"].(string), | ||
| StrictTransportSecurity: headerSettings["strict_transport_security"].(string), | ||
|
|
@@ -1008,8 +1009,6 @@ func getRealmFromData(data *schema.ResourceData, keycloakVersion *version.Versio | |
| XXSSProtection: headerSettings["x_xss_protection"].(string), | ||
| ReferrerPolicy: headerSettings["referrer_policy"].(string), | ||
| } | ||
| } else { | ||
| setDefaultSecuritySettingHeaders(realm) | ||
| } | ||
|
|
||
| bruteForceDetectionConfig := securityDefensesSettings["brute_force_detection"].([]interface{}) | ||
|
|
@@ -1027,7 +1026,6 @@ func getRealmFromData(data *schema.ResourceData, keycloakVersion *version.Versio | |
| setDefaultSecuritySettingsBruteForceDetection(realm) | ||
| } | ||
| } else { | ||
| setDefaultSecuritySettingHeaders(realm) | ||
| setDefaultSecuritySettingsBruteForceDetection(realm) | ||
| } | ||
|
|
||
|
|
@@ -1178,7 +1176,7 @@ func getRealmFromData(data *schema.ResourceData, keycloakVersion *version.Versio | |
| } | ||
|
|
||
| func setDefaultSecuritySettingHeaders(realm *keycloak.Realm) { | ||
| realm.BrowserSecurityHeaders = keycloak.BrowserSecurityHeaders{ | ||
| realm.BrowserSecurityHeaders = &keycloak.BrowserSecurityHeaders{ | ||
| ContentSecurityPolicy: "frame-src 'self'; frame-ancestors 'self'; object-src 'none';", | ||
| ContentSecurityPolicyReportOnly: "", | ||
| StrictTransportSecurity: "max-age=31536000; includeSubDomains", | ||
|
|
@@ -1309,6 +1307,14 @@ func setRealmData(data *schema.ResourceData, realm *keycloak.Realm, keycloakVers | |
| securityDefensesSettings["brute_force_detection"] = []interface{}{getBruteForceDetectionSettings(realm)} | ||
| data.Set("security_defenses", []interface{}{securityDefensesSettings}) | ||
| } | ||
| } else { | ||
| // if we do not have any configuration in the realm config, use the default config generated by Keycloak | ||
| securityDefensesSettings := make(map[string]interface{}) | ||
| securityDefensesSettings["headers"] = []interface{}{getHeaderSettings(realm)} | ||
| if realm.BruteForceProtected { | ||
| securityDefensesSettings["brute_force_detection"] = []interface{}{getBruteForceDetectionSettings(realm)} | ||
| } | ||
| data.Set("security_defenses", []interface{}{securityDefensesSettings}) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This populates the tfstate with the security defenses defaults configured generated by Keycloak. |
||
| } | ||
|
|
||
| data.Set("password_policy", realm.PasswordPolicy) | ||
|
|
@@ -1392,6 +1398,11 @@ func getBruteForceDetectionSettings(realm *keycloak.Realm) map[string]interface{ | |
| } | ||
|
|
||
| func getHeaderSettings(realm *keycloak.Realm) map[string]interface{} { | ||
|
|
||
| if realm.BrowserSecurityHeaders == nil { | ||
| return nil | ||
| } | ||
|
|
||
| headersSettings := make(map[string]interface{}) | ||
| headersSettings["content_security_policy"] = realm.BrowserSecurityHeaders.ContentSecurityPolicy | ||
| headersSettings["content_security_policy_report_only"] = realm.BrowserSecurityHeaders.ContentSecurityPolicyReportOnly | ||
|
|
@@ -1431,7 +1442,7 @@ func resourceKeycloakRealmCreate(ctx context.Context, data *schema.ResourceData, | |
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| setRealmData(data, realm, keycloakVersion) | ||
| data.SetId(realm.Realm) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The call to setRealmData(...) was not necessary as it is already done by |
||
|
|
||
| return resourceKeycloakRealmRead(ctx, data, meta) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
omitempty allows us to leverage the BrowserSecurityHeader defaults from Keycloak.