Skip to content

Commit a5d8706

Browse files
add datasource for account idp and update docs
1 parent 0302193 commit a5d8706

File tree

10 files changed

+429
-292
lines changed

10 files changed

+429
-292
lines changed

codefresh/data_account_idp.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package codefresh
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/codefresh-io/terraform-provider-codefresh/codefresh/cfclient"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
)
9+
10+
func dataSourceAccountIdp() *schema.Resource {
11+
return &schema.Resource{
12+
Description: "This data source retrieves an account level identity provider",
13+
Read: dataSourceAccountIdpRead,
14+
Schema: AccountIdpSchema(),
15+
}
16+
}
17+
18+
// IdpSchema -
19+
func AccountIdpSchema() map[string]*schema.Schema {
20+
return map[string]*schema.Schema{
21+
"_id": {
22+
Type: schema.TypeString,
23+
Optional: true,
24+
ExactlyOneOf: []string{"_id", "client_name"},
25+
},
26+
"client_name": {
27+
Type: schema.TypeString,
28+
Optional: true,
29+
ExactlyOneOf: []string{"_id", "client_name"},
30+
},
31+
"display_name": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
"client_type": {
36+
Type: schema.TypeString,
37+
Computed: true,
38+
},
39+
"redirect_url": {
40+
Description: "API Callback url for the identity provider",
41+
Type: schema.TypeString,
42+
Computed: true,
43+
},
44+
"redirect_ui_url": {
45+
Description: "UI Callback url for the identity provider",
46+
Type: schema.TypeString,
47+
Computed: true,
48+
},
49+
"login_url": {
50+
Description: "Login url using the IDP to Codefresh",
51+
Type: schema.TypeString,
52+
Computed: true,
53+
},
54+
}
55+
}
56+
57+
func dataSourceAccountIdpRead(d *schema.ResourceData, meta interface{}) error {
58+
59+
client := meta.(*cfclient.Client)
60+
61+
idps, err := client.GetAccountIDPs()
62+
if err != nil {
63+
return err
64+
}
65+
66+
_id, _idOk := d.GetOk("_id")
67+
clientName, clientNameOk := d.GetOk("client_name")
68+
69+
for _, idp := range *idps {
70+
if clientNameOk && clientName.(string) != idp.ClientName {
71+
continue
72+
}
73+
if _idOk && _id.(string) != idp.ID {
74+
continue
75+
}
76+
77+
err = mapDataAccountIdpToResource(idp, d)
78+
if err != nil {
79+
return err
80+
}
81+
}
82+
83+
if d.Id() == "" {
84+
return fmt.Errorf("[EROOR] Idp wasn't found")
85+
}
86+
87+
return nil
88+
}
89+
90+
func mapDataAccountIdpToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) error {
91+
92+
d.SetId(cfClientIDP.ID)
93+
d.Set("client_name", cfClientIDP.ClientName)
94+
d.Set("client_type", cfClientIDP.ClientType)
95+
d.Set("display_name", cfClientIDP.DisplayName)
96+
d.Set("redirect_url", cfClientIDP.RedirectUrl)
97+
d.Set("redirect_ui_url", cfClientIDP.RedirectUiUrl)
98+
d.Set("login_url", cfClientIDP.LoginUrl)
99+
100+
return nil
101+
}

codefresh/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func Provider() *schema.Provider {
5151
"codefresh_users": dataSourceUsers(),
5252
"codefresh_registry": dataSourceRegistry(),
5353
"codefresh_pipelines": dataSourcePipelines(),
54+
"codefresh_account_idp": dataSourceAccountIdp(),
5455
},
5556
ResourcesMap: map[string]*schema.Resource{
5657
"codefresh_account": resourceAccount(),

codefresh/resource_account_idp.go

Lines changed: 41 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
func resourceAccountIdp() *schema.Resource {
1616
return &schema.Resource{
17-
Description: "Identity providers used in Codefresh for user authentication.",
17+
Description: "Account level identity providers",
1818
Create: resourceAccountIDPCreate,
1919
Read: resourceAccountIDPRead,
2020
Update: resourceAccountIDPUpdate,
@@ -142,42 +142,37 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
142142
// Codefresh API Returns the client secret as an encrypted string on the server side
143143
// hence we need to keep in the state the original secret the user provides along with the encrypted computed secret
144144
// for Terraform to properly calculate the diff
145-
"client_secret": d.Get("github.0.client_secret"),
146-
"client_secret_encrypted": cfClientIDP.ClientSecret,
147-
"authentication_url": cfClientIDP.AuthURL,
148-
"token_url": cfClientIDP.TokenURL,
149-
"user_profile_url": cfClientIDP.UserProfileURL,
150-
"api_host": cfClientIDP.ApiHost,
151-
"api_path_prefix": cfClientIDP.ApiPathPrefix,
145+
"client_secret": d.Get("github.0.client_secret"),
146+
"authentication_url": cfClientIDP.AuthURL,
147+
"token_url": cfClientIDP.TokenURL,
148+
"user_profile_url": cfClientIDP.UserProfileURL,
149+
"api_host": cfClientIDP.ApiHost,
150+
"api_path_prefix": cfClientIDP.ApiPathPrefix,
152151
}}
153152

154153
d.Set("github", attributes)
155154
}
156155

157156
if cfClientIDP.ClientType == "gitlab" {
158157
attributes := []map[string]interface{}{{
159-
"client_id": cfClientIDP.ClientId,
160-
"client_secret": d.Get("gitlab.0.client_secret"),
161-
"client_secret_encrypted": cfClientIDP.ClientSecret,
162-
"authentication_url": cfClientIDP.AuthURL,
163-
"user_profile_url": cfClientIDP.UserProfileURL,
164-
"api_url": cfClientIDP.ApiURL,
158+
"client_id": cfClientIDP.ClientId,
159+
"client_secret": d.Get("gitlab.0.client_secret"),
160+
"authentication_url": cfClientIDP.AuthURL,
161+
"user_profile_url": cfClientIDP.UserProfileURL,
162+
"api_url": cfClientIDP.ApiURL,
165163
}}
166164

167165
d.Set("gitlab", attributes)
168166
}
169167

170168
if cfClientIDP.ClientType == "okta" {
171169
attributes := []map[string]interface{}{{
172-
"client_id": cfClientIDP.ClientId,
173-
"client_secret": d.Get("okta.0.client_secret"),
174-
"client_secret_encrypted": cfClientIDP.ClientSecret,
175-
"client_host": cfClientIDP.ClientHost,
176-
"app_id": d.Get("okta.0.app_id"),
177-
"app_id_encrypted": cfClientIDP.AppId,
178-
"sync_mirror_accounts": cfClientIDP.SyncMirrorAccounts,
179-
"access_token": d.Get("okta.0.access_token"),
180-
"access_token_encrypted": cfClientIDP.Access_token,
170+
"client_id": cfClientIDP.ClientId,
171+
"client_secret": d.Get("okta.0.client_secret"),
172+
"client_host": cfClientIDP.ClientHost,
173+
"app_id": d.Get("okta.0.app_id"),
174+
"sync_mirror_accounts": cfClientIDP.SyncMirrorAccounts,
175+
"access_token": d.Get("okta.0.access_token"),
181176
}}
182177

183178
d.Set("okta", attributes)
@@ -187,11 +182,8 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
187182
attributes := []map[string]interface{}{{
188183
"client_id": cfClientIDP.ClientId,
189184
"client_secret": d.Get("google.0.client_secret"),
190-
"client_secret_encrypted": cfClientIDP.ClientSecret,
191185
"admin_email": d.Get("google.0.admin_email"),
192-
"admin_email_encrypted": cfClientIDP.Subject,
193186
"json_keyfile": d.Get("google.0.json_keyfile"),
194-
"json_keyfile_encrypted": cfClientIDP.KeyFile,
195187
"allowed_groups_for_sync": cfClientIDP.AllowedGroupsForSync,
196188
"sync_field": cfClientIDP.SyncField,
197189
}}
@@ -201,10 +193,9 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
201193

202194
if cfClientIDP.ClientType == "auth0" {
203195
attributes := []map[string]interface{}{{
204-
"client_id": cfClientIDP.ClientId,
205-
"client_secret": d.Get("auth0.0.client_secret"),
206-
"client_secret_encrypted": cfClientIDP.ClientSecret,
207-
"domain": cfClientIDP.ClientHost,
196+
"client_id": cfClientIDP.ClientId,
197+
"client_secret": d.Get("auth0.0.client_secret"),
198+
"domain": cfClientIDP.ClientHost,
208199
}}
209200

210201
d.Set("auth0", attributes)
@@ -221,7 +212,6 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
221212
attributes := []map[string]interface{}{{
222213
"app_id": cfClientIDP.ClientId,
223214
"client_secret": d.Get("azure.0.client_secret"),
224-
"client_secret_encrypted": cfClientIDP.ClientSecret,
225215
"object_id": cfClientIDP.AppId,
226216
"autosync_teams_and_users": cfClientIDP.AutoGroupSync,
227217
"sync_interval": syncInterval,
@@ -233,11 +223,10 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
233223

234224
if cfClientIDP.ClientType == "onelogin" {
235225
attributes := []map[string]interface{}{{
236-
"client_id": cfClientIDP.ClientId,
237-
"client_secret": d.Get("onelogin.0.client_secret"),
238-
"client_secret_encrypted": cfClientIDP.ClientSecret,
239-
"domain": cfClientIDP.ClientHost,
240-
"api_client_id": cfClientIDP.ApiClientId,
226+
"client_id": cfClientIDP.ClientId,
227+
"client_secret": d.Get("onelogin.0.client_secret"),
228+
"domain": cfClientIDP.ClientHost,
229+
"api_client_id": cfClientIDP.ApiClientId,
241230
// When account scoped, Client secret is returned obfuscated after first apply, causing diff to appear everytime.
242231
// This behavior would always set the API clint secret from the resource, allowing at least changing the secret when the value in terraform configuration changes.
243232
// Though it would not detect drift if the secret is changed from UI.
@@ -250,11 +239,10 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
250239

251240
if cfClientIDP.ClientType == "keycloak" {
252241
attributes := []map[string]interface{}{{
253-
"client_id": cfClientIDP.ClientId,
254-
"client_secret": d.Get("keycloak.0.client_secret"),
255-
"client_secret_encrypted": cfClientIDP.ClientSecret,
256-
"host": cfClientIDP.Host,
257-
"realm": cfClientIDP.Realm,
242+
"client_id": cfClientIDP.ClientId,
243+
"client_secret": d.Get("keycloak.0.client_secret"),
244+
"host": cfClientIDP.Host,
245+
"realm": cfClientIDP.Realm,
258246
}}
259247

260248
d.Set("keycloak", attributes)
@@ -267,22 +255,18 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
267255
return err
268256
}
269257
attributes := []map[string]interface{}{{
270-
"endpoint": cfClientIDP.EntryPoint,
271-
"application_certificate": d.Get("saml.0.application_certificate"),
272-
"application_certificate_encrypted": cfClientIDP.ApplicationCert,
273-
"provider": cfClientIDP.SamlProvider,
274-
"allowed_groups_for_sync": cfClientIDP.AllowedGroupsForSync,
275-
"autosync_teams_and_users": cfClientIDP.AutoGroupSync,
276-
"activate_users_after_sync": cfClientIDP.ActivateUserAfterSync,
277-
"sync_interval": syncInterval,
278-
"app_id": cfClientIDP.AppId,
279-
"client_host": cfClientIDP.ClientHost,
280-
"json_keyfile": d.Get("saml.0.json_keyfile"),
281-
"json_keyfile_encrypted": cfClientIDP.KeyFile,
282-
"admin_email": d.Get("saml.0.admin_email"),
283-
"admin_email_encrypted": cfClientIDP.Subject,
284-
"access_token": d.Get("saml.0.access_token"),
285-
"access_token_encrypted": cfClientIDP.Access_token,
258+
"endpoint": cfClientIDP.EntryPoint,
259+
"application_certificate": d.Get("saml.0.application_certificate"),
260+
"provider": cfClientIDP.SamlProvider,
261+
"allowed_groups_for_sync": cfClientIDP.AllowedGroupsForSync,
262+
"autosync_teams_and_users": cfClientIDP.AutoGroupSync,
263+
"activate_users_after_sync": cfClientIDP.ActivateUserAfterSync,
264+
"sync_interval": syncInterval,
265+
"app_id": cfClientIDP.AppId,
266+
"client_host": cfClientIDP.ClientHost,
267+
"json_keyfile": d.Get("saml.0.json_keyfile"),
268+
"admin_email": d.Get("saml.0.admin_email"),
269+
"access_token": d.Get("saml.0.access_token"),
286270
}}
287271

288272
d.Set("saml", attributes)
@@ -292,12 +276,10 @@ func mapAccountIDPToResource(cfClientIDP cfclient.IDP, d *schema.ResourceData) e
292276
attributes := []map[string]interface{}{{
293277
"url": cfClientIDP.Url,
294278
"password": d.Get("ldap.0.password"),
295-
"password_encrypted": cfClientIDP.Password,
296279
"distinguished_name": cfClientIDP.DistinguishedName,
297280
"search_base": cfClientIDP.SearchBase,
298281
"search_filter": cfClientIDP.SearchFilter,
299282
"certificate": d.Get("ldap.0.certificate"),
300-
"certificate_encrypted": cfClientIDP.Certificate,
301283
"allowed_groups_for_sync": cfClientIDP.AllowedGroupsForSync,
302284
"search_base_for_sync": cfClientIDP.SearchBaseForSync,
303285
}}

codefresh/resource_account_idp_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func testAccountIDPCodefreshConfig(idpType string, uniqueId string) string {
242242
app_id = "test1"
243243
access_token = "myaccesstoken1"
244244
}
245-
}`, uniqueId,uniqueId)
245+
}`, uniqueId, uniqueId)
246246
}
247247

248248
if idpType == "saml" {
@@ -271,7 +271,7 @@ func testAccountIDPCodefreshConfig(idpType string, uniqueId string) string {
271271
-----END CERTIFICATE-----
272272
EOT
273273
}
274-
}`, uniqueId,uniqueId)
274+
}`, uniqueId, uniqueId)
275275
}
276276

277277
return idpResource

0 commit comments

Comments
 (0)