generated from deploymenttheory/Template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from deploymenttheory/feature-scaffolding
added certificate validators and consts for various ms cloud types
- Loading branch information
Showing
5 changed files
with
216 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package constants | ||
|
||
const ( | ||
// Public Cloud | ||
PUBLIC_OAUTH_AUTHORITY_URL = "https://login.microsoftonline.com/" | ||
PUBLIC_GRAPH_API_SCOPE = "https://graph.microsoft.com/.default" | ||
PUBLIC_GRAPH_API_DOMAIN = "graph.microsoft.com" | ||
|
||
// US Department of Defense (DoD) Cloud | ||
USDOD_OAUTH_AUTHORITY_URL = "https://login.microsoftonline.us/" | ||
USDOD_GRAPH_API_SCOPE = "https://graph.microsoft.us/.default" | ||
USDOD_GRAPH_API_DOMAIN = "graph.microsoft.us" | ||
|
||
// US Government Cloud | ||
USGOV_OAUTH_AUTHORITY_URL = "https://login.microsoftonline.com/" | ||
USGOV_GRAPH_API_SCOPE = "https://graph.microsoft.us/.default" | ||
USGOV_GRAPH_API_DOMAIN = "graph.microsoft.us" | ||
|
||
// US Government High Cloud | ||
USGOVHIGH_OAUTH_AUTHORITY_URL = "https://login.microsoftonline.us/" | ||
USGOVHIGH_GRAPH_API_SCOPE = "https://graph.microsoft.us/.default" | ||
USGOVHIGH_GRAPH_API_DOMAIN = "graph.microsoft.us" | ||
|
||
// China Cloud - https://learn.microsoft.com/en-us/previous-versions/office/office-365-api/api/o365-china-endpoints | ||
CHINA_OAUTH_AUTHORITY_URL = "https://login.chinacloudapi.cn/" | ||
CHINA_GRAPH_API_SCOPE = "https://microsoftgraph.chinacloudapi.cn/.default" | ||
CHINA_GRAPH_API_DOMAIN = "microsoftgraph.chinacloudapi.cn" | ||
|
||
// EagleX Cloud | ||
EX_OAUTH_AUTHORITY_URL = "https://login.microsoftonline.eaglex.ic.gov/" | ||
EX_GRAPH_API_SCOPE = "https://graph.eaglex.ic.gov/.default" | ||
EX_GRAPH_API_DOMAIN = "graph.eaglex.ic.gov" | ||
EX_AUTHORITY_HOST = "https://login.microsoftonline.eaglex.ic.gov/" | ||
|
||
// Secure Cloud (RX) | ||
RX_OAUTH_AUTHORITY_URL = "https://login.microsoftonline.microsoft.scloud/" | ||
RX_GRAPH_API_SCOPE = "https://graph.microsoft.scloud/.default" | ||
RX_GRAPH_API_DOMAIN = "graph.microsoft.scloud" | ||
RX_AUTHORITY_HOST = "https://login.microsoftonline.microsoft.scloud/" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package provider | ||
|
||
import ( | ||
"context" | ||
"crypto/x509" | ||
"fmt" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
azidentity "github.com/Azure/azure-sdk-for-go/sdk/azidentity" | ||
"github.com/deploymenttheory/terraform-provider-microsoft365/internal/helpers" | ||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
) | ||
|
||
// createCredential creates an Azure credential based on the provider configuration. | ||
func createCredential(ctx context.Context, data M365ProviderModel, clientOptions policy.ClientOptions) (azcore.TokenCredential, error) { | ||
switch data.AuthMethod.ValueString() { | ||
case "device_code": | ||
tflog.Debug(ctx, "Creating DeviceCodeCredential", map[string]interface{}{ | ||
"tenant_id": data.TenantID.ValueString(), | ||
"client_id": data.ClientID.ValueString(), | ||
}) | ||
return azidentity.NewDeviceCodeCredential(&azidentity.DeviceCodeCredentialOptions{ | ||
TenantID: data.TenantID.ValueString(), | ||
ClientID: data.ClientID.ValueString(), | ||
UserPrompt: func(ctx context.Context, message azidentity.DeviceCodeMessage) error { | ||
tflog.Info(ctx, message.Message) | ||
return nil | ||
}, | ||
ClientOptions: clientOptions, | ||
}) | ||
case "client_secret": | ||
tflog.Debug(ctx, "Creating ClientSecretCredential", map[string]interface{}{ | ||
"tenant_id": data.TenantID.ValueString(), | ||
"client_id": data.ClientID.ValueString(), | ||
}) | ||
return azidentity.NewClientSecretCredential(data.TenantID.ValueString(), data.ClientID.ValueString(), data.ClientSecret.ValueString(), &azidentity.ClientSecretCredentialOptions{ | ||
ClientOptions: clientOptions, | ||
}) | ||
case "client_certificate": | ||
tflog.Debug(ctx, "Creating ClientCertificateCredential", map[string]interface{}{ | ||
"tenant_id": data.TenantID.ValueString(), | ||
"client_id": data.ClientID.ValueString(), | ||
}) | ||
|
||
var certs []*x509.Certificate | ||
var key interface{} | ||
var err error | ||
|
||
if !data.ClientCertificate.IsNull() { | ||
tflog.Debug(ctx, "Using base64 encoded client certificate") | ||
certs, key, err = helpers.GetCertificatesAndKeyFromCertOrFilePath(data.ClientCertificate.ValueString(), data.ClientCertificatePassword.ValueString()) | ||
} else if !data.ClientCertificateFilePath.IsNull() { | ||
tflog.Debug(ctx, "Using client certificate file path") | ||
certs, key, err = helpers.GetCertificatesAndKeyFromCertOrFilePath(data.ClientCertificateFilePath.ValueString(), data.ClientCertificatePassword.ValueString()) | ||
} else { | ||
return nil, fmt.Errorf("either 'client_certificate' or 'client_certificate_file_path' must be provided for client_certificate authentication") | ||
} | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("failed to get certificates and key: %s", err.Error()) | ||
} | ||
|
||
return azidentity.NewClientCertificateCredential(data.TenantID.ValueString(), data.ClientID.ValueString(), certs, key, &azidentity.ClientCertificateCredentialOptions{ | ||
ClientOptions: clientOptions, | ||
}) | ||
case "on_behalf_of": | ||
tflog.Debug(ctx, "Creating OnBehalfOfCredentialWithSecret", map[string]interface{}{ | ||
"tenant_id": data.TenantID.ValueString(), | ||
"client_id": data.ClientID.ValueString(), | ||
}) | ||
userAssertion := data.UserAssertion.ValueString() | ||
return azidentity.NewOnBehalfOfCredentialWithSecret(data.TenantID.ValueString(), data.ClientID.ValueString(), userAssertion, data.ClientSecret.ValueString(), &azidentity.OnBehalfOfCredentialOptions{ | ||
ClientOptions: clientOptions, | ||
}) | ||
case "interactive_browser": | ||
tflog.Debug(ctx, "Creating InteractiveBrowserCredential", map[string]interface{}{ | ||
"tenant_id": data.TenantID.ValueString(), | ||
"client_id": data.ClientID.ValueString(), | ||
"redirect_url": data.RedirectURL.ValueString(), | ||
}) | ||
redirectURL := data.RedirectURL.ValueString() | ||
return azidentity.NewInteractiveBrowserCredential(&azidentity.InteractiveBrowserCredentialOptions{ | ||
TenantID: data.TenantID.ValueString(), | ||
ClientID: data.ClientID.ValueString(), | ||
RedirectURL: redirectURL, | ||
ClientOptions: clientOptions, | ||
}) | ||
case "username_password": | ||
tflog.Debug(ctx, "Creating UsernamePasswordCredential", map[string]interface{}{ | ||
"tenant_id": data.TenantID.ValueString(), | ||
"client_id": data.ClientID.ValueString(), | ||
}) | ||
username := data.Username.ValueString() | ||
password := data.Password.ValueString() | ||
return azidentity.NewUsernamePasswordCredential(data.TenantID.ValueString(), data.ClientID.ValueString(), username, password, &azidentity.UsernamePasswordCredentialOptions{ | ||
ClientOptions: clientOptions, | ||
}) | ||
default: | ||
return nil, fmt.Errorf("unsupported authentication method '%s'", data.AuthMethod.ValueString()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.