-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
202 lines (167 loc) · 6.83 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# Get Unique ID in keycloak
data "keycloak_realm" "realm" {
realm = var.kc_realm
}
##############
## IDIR IDP ##
##############
resource "keycloak_oidc_identity_provider" "idir_identity_provider" {
realm = data.keycloak_realm.realm.id
provider_id = "oidc"
alias = "azureidir"
display_name = "IDIR - MFA"
enabled = true
store_token = false
first_broker_login_flow_alias = "first broker login"
sync_mode = "FORCE"
authorization_url = "${var.kc_base_url}/realms/standard/protocol/openid-connect/auth?kc_idp_hint=azureidir"
token_url = "${var.kc_base_url}/realms/standard/protocol/openid-connect/token"
logout_url = "${var.kc_base_url}/realms/standard/protocol/openid-connect/logout"
backchannel_supported = false
user_info_url = "${var.kc_base_url}/realms/standard/protocol/openid-connect/userinfo"
client_id = var.client_id
client_secret = var.client_secret
# issuer = "${var.kc_base_url}/realms/_azureidir"
default_scopes = "openid profile"
validate_signature = true
jwks_url = "${var.kc_base_url}/realms/standard/protocol/openid-connect/certs"
extra_config = {
"clientAuthMethod" = "client_secret_post"
}
}
############
## Mapper ##
############
resource "keycloak_custom_identity_provider_mapper" "new_idir_displayname" {
realm = data.keycloak_realm.realm.id
name = "display_name"
identity_provider_alias = keycloak_oidc_identity_provider.idir_identity_provider.alias
identity_provider_mapper = "oidc-user-attribute-idp-mapper"
extra_config = {
"syncMode" = "FORCE"
"claim" = "display_name"
"user.attribute" = "display_name"
}
}
resource "keycloak_custom_identity_provider_mapper" "new_idir_idir_userid" {
realm = data.keycloak_realm.realm.id
name = "idir_user_guid"
identity_provider_alias = keycloak_oidc_identity_provider.idir_identity_provider.alias
identity_provider_mapper = "oidc-user-attribute-idp-mapper"
extra_config = {
"syncMode" = "FORCE"
"claim" = "idir_user_guid"
"user.attribute" = "idir_user_guid"
}
}
resource "keycloak_custom_identity_provider_mapper" "new_idir_idir_guid" {
realm = data.keycloak_realm.realm.id
name = "idir_username"
identity_provider_alias = keycloak_oidc_identity_provider.idir_identity_provider.alias
identity_provider_mapper = "oidc-user-attribute-idp-mapper"
extra_config = {
"syncMode" = "FORCE"
"claim" = "idir_username"
"user.attribute" = "idir_username"
}
}
############
## client ##
############
resource "keycloak_saml_client" "amazon" {
realm_id = data.keycloak_realm.realm.id
client_id = "urn:amazon:webservices"
name = var.saml_client_display_name
sign_documents = true
sign_assertions = true
include_authn_statement = true
name_id_format = "persistent"
signature_algorithm = "RSA_SHA256"
signature_key_name = "KEY_ID"
canonicalization_method = "EXCLUSIVE"
valid_redirect_uris = [
"https://signin.aws.amazon.com/saml",
"${var.cloudfront_auth_url}",
"/auth/realms/${var.kc_realm}/protocol/saml/clients/amazon-aws"
]
base_url = "/auth/realms/${var.kc_realm}/protocol/saml/clients/amazon-aws"
idp_initiated_sso_url_name = "amazon-aws"
assertion_consumer_post_url = var.cloudfront_auth_url
full_scope_allowed = false
extra_config = {
"saml.assertion.lifespan" = 300
}
}
resource "keycloak_generic_client_protocol_mapper" "amazon_mapper_session_name" {
realm_id = data.keycloak_realm.realm.id
client_id = keycloak_saml_client.amazon.id
protocol = "saml"
name = "Session Name"
protocol_mapper = "saml-user-property-mapper"
config = {
"user.attribute" = "username"
"friendly.name" = "Session Name"
"attribute.nameformat" = "Basic"
"attribute.name" = "https://aws.amazon.com/SAML/Attributes/RoleSessionName"
}
}
resource "keycloak_generic_client_protocol_mapper" "amazon_mapper_session_role" {
realm_id = data.keycloak_realm.realm.id
client_id = keycloak_saml_client.amazon.id
protocol = "saml"
name = "https://aws.amazon.com/SAML/Attributes/Role"
protocol_mapper = "saml-role-list-mapper"
config = {
"single" = true
"friendly.name" = "Session Role"
"attribute.nameformat" = "Basic"
"attribute.name" = "https://aws.amazon.com/SAML/Attributes/Role"
}
}
resource "keycloak_generic_client_protocol_mapper" "amazon_mapper_session_duration" {
realm_id = data.keycloak_realm.realm.id
client_id = keycloak_saml_client.amazon.id
protocol = "saml"
name = "Session Duration"
protocol_mapper = "saml-hardcode-attribute-mapper"
config = {
"attribute.value" = 28800
"friendly.name" = "Session Duration"
"attribute.nameformat" = "Basic"
"attribute.name" = "https://aws.amazon.com/SAML/Attributes/SessionDuration"
}
}
resource "keycloak_openid_client" "user_managemennt_client" {
client_id = "user_management"
name = "user_management"
realm_id = data.keycloak_realm.realm.id
description = "Client with Scoped perms to manage users through registry"
enabled = true
full_scope_allowed = false
standard_flow_enabled = false
service_accounts_enabled = true
backchannel_logout_session_required = true
access_type = "CONFIDENTIAL"
}
data "keycloak_openid_client" "realm_management" {
realm_id = data.keycloak_realm.realm.id
client_id = "realm-management"
}
data "keycloak_role" "roles" {
for_each = toset(var.role_names)
realm_id = data.keycloak_realm.realm.id
client_id = data.keycloak_openid_client.realm_management.id
name = each.value
}
resource "keycloak_role" "user_management_roles" {
name = "user_management_roles"
realm_id = data.keycloak_realm.realm.id
client_id = keycloak_openid_client.user_managemennt_client.id
composite_roles = [for role in var.role_names : data.keycloak_role.roles[role].id]
}
resource "keycloak_openid_client_service_account_role" "user_managemennt_service_account_role" {
realm_id = data.keycloak_realm.realm.id
service_account_user_id = keycloak_openid_client.user_managemennt_client.service_account_user_id
client_id = keycloak_openid_client.user_managemennt_client.id
role = keycloak_role.user_management_roles.name
}