-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
49 lines (40 loc) · 1.36 KB
/
provider.go
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
package model
import (
"os"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
)
type Provider struct {
TenantId types.String `tfsdk:"tenant_id"`
ClientId types.String `tfsdk:"client_id"`
ClientSecret types.String `tfsdk:"client_secret"`
}
func (m Provider) GetCredential() (azcore.TokenCredential, diag.Diagnostics) {
var d diag.Diagnostics
tid := m.GetWithDefault(m.TenantId, "B2C_ARM_TENANT_ID")
if tid == "" {
d.AddError("missing tenant_id", "must be configured or provided via B2C_ARM_TENANT_ID env var")
}
cid := m.GetWithDefault(m.ClientId, "B2C_ARM_CLIENT_ID")
if cid == "" {
d.AddError("missing client_id", "must be configured or provided via B2C_ARM_CLIENT_ID env var")
}
cs := m.GetWithDefault(m.ClientSecret, "B2C_ARM_CLIENT_SECRET")
if cs == "" {
d.AddError("missing client_secret", "must be configured or provided via B2C_ARM_CLIENT_SECRET env var")
}
cred, err := azidentity.NewClientSecretCredential(tid, cid, cs, nil)
if err != nil {
d.AddError("invalid azure ad b2c client credentials", err.Error())
}
return cred, d
}
func (m Provider) GetWithDefault(attr types.String, env string) string {
v := attr.ValueString()
if v != "" {
return v
}
return os.Getenv(env)
}