|
| 1 | +package azuread |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore" |
| 10 | + "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" |
| 11 | + "github.com/Azure/azure-sdk-for-go/sdk/azidentity" |
| 12 | + mssql "github.com/denisenkom/go-mssqldb" |
| 13 | + "github.com/denisenkom/go-mssqldb/msdsn" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + ActiveDirectoryDefault = "ActiveDirectoryDefault" |
| 18 | + ActiveDirectoryIntegrated = "ActiveDirectoryIntegrated" |
| 19 | + ActiveDirectoryPassword = "ActiveDirectoryPassword" |
| 20 | + ActiveDirectoryInteractive = "ActiveDirectoryInteractive" |
| 21 | + // ActiveDirectoryMSI is a synonym for ActiveDirectoryManagedIdentity |
| 22 | + ActiveDirectoryMSI = "ActiveDirectoryMSI" |
| 23 | + ActiveDirectoryManagedIdentity = "ActiveDirectoryManagedIdentity" |
| 24 | + // ActiveDirectoryApplication is a synonym for ActiveDirectoryServicePrincipal |
| 25 | + ActiveDirectoryApplication = "ActiveDirectoryApplication" |
| 26 | + ActiveDirectoryServicePrincipal = "ActiveDirectoryServicePrincipal" |
| 27 | + scopeDefaultSuffix = "/.default" |
| 28 | +) |
| 29 | + |
| 30 | +type azureFedAuthConfig struct { |
| 31 | + adalWorkflow byte |
| 32 | + mssqlConfig msdsn.Config |
| 33 | + // The detected federated authentication library |
| 34 | + fedAuthLibrary int |
| 35 | + fedAuthWorkflow string |
| 36 | + // Service principal logins |
| 37 | + clientID string |
| 38 | + tenantID string |
| 39 | + clientSecret string |
| 40 | + certificatePath string |
| 41 | + |
| 42 | + // AD password/managed identity/interactive |
| 43 | + user string |
| 44 | + password string |
| 45 | + applicationClientID string |
| 46 | +} |
| 47 | + |
| 48 | +// parse returns a config based on an msdsn-style connection string |
| 49 | +func parse(dsn string) (*azureFedAuthConfig, error) { |
| 50 | + mssqlConfig, params, err := msdsn.Parse(dsn) |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + config := &azureFedAuthConfig{ |
| 55 | + fedAuthLibrary: mssql.FedAuthLibraryReserved, |
| 56 | + mssqlConfig: mssqlConfig, |
| 57 | + } |
| 58 | + |
| 59 | + err = config.validateParameters(params) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + return config, nil |
| 65 | +} |
| 66 | + |
| 67 | +func (p *azureFedAuthConfig) validateParameters(params map[string]string) error { |
| 68 | + |
| 69 | + fedAuthWorkflow, _ := params["fedauth"] |
| 70 | + if fedAuthWorkflow == "" { |
| 71 | + return nil |
| 72 | + } |
| 73 | + |
| 74 | + p.fedAuthLibrary = mssql.FedAuthLibraryADAL |
| 75 | + |
| 76 | + p.applicationClientID, _ = params["applicationclientid"] |
| 77 | + |
| 78 | + switch { |
| 79 | + case strings.EqualFold(fedAuthWorkflow, ActiveDirectoryPassword): |
| 80 | + if p.applicationClientID == "" { |
| 81 | + return errors.New("applicationclientid parameter is required for " + ActiveDirectoryPassword) |
| 82 | + } |
| 83 | + p.adalWorkflow = mssql.FedAuthADALWorkflowPassword |
| 84 | + p.user, _ = params["user id"] |
| 85 | + p.password, _ = params["password"] |
| 86 | + case strings.EqualFold(fedAuthWorkflow, ActiveDirectoryIntegrated): |
| 87 | + // Active Directory Integrated authentication is not fully supported: |
| 88 | + // you can only use this by also implementing an a token provider |
| 89 | + // and supplying it via ActiveDirectoryTokenProvider in the Connection. |
| 90 | + p.adalWorkflow = mssql.FedAuthADALWorkflowIntegrated |
| 91 | + case strings.EqualFold(fedAuthWorkflow, ActiveDirectoryManagedIdentity) || strings.EqualFold(fedAuthWorkflow, ActiveDirectoryMSI): |
| 92 | + // When using MSI, to request a specific client ID or user-assigned identity, |
| 93 | + // provide the ID in the "user id" parameter |
| 94 | + p.adalWorkflow = mssql.FedAuthADALWorkflowMSI |
| 95 | + p.clientID, _ = splitTenantAndClientID(params["user id"]) |
| 96 | + case strings.EqualFold(fedAuthWorkflow, ActiveDirectoryApplication) || strings.EqualFold(fedAuthWorkflow, ActiveDirectoryServicePrincipal): |
| 97 | + p.adalWorkflow = mssql.FedAuthADALWorkflowPassword |
| 98 | + // Split the clientID@tenantID format |
| 99 | + // If no tenant is provided we'll use the one from the server |
| 100 | + p.clientID, p.tenantID = splitTenantAndClientID(params["user id"]) |
| 101 | + if p.clientID == "" { |
| 102 | + return errors.New("Must provide 'client id[@tenant id]' as username parameter when using ActiveDirectoryApplication authentication") |
| 103 | + } |
| 104 | + |
| 105 | + p.clientSecret, _ = params["password"] |
| 106 | + |
| 107 | + p.certificatePath, _ = params["clientcertpath"] |
| 108 | + |
| 109 | + if p.certificatePath == "" && p.clientSecret == "" { |
| 110 | + return errors.New("Must provide 'password' parameter when using ActiveDirectoryApplication authentication without cert/key credentials") |
| 111 | + } |
| 112 | + case strings.EqualFold(fedAuthWorkflow, ActiveDirectoryDefault): |
| 113 | + p.adalWorkflow = mssql.FedAuthADALWorkflowPassword |
| 114 | + case strings.EqualFold(fedAuthWorkflow, ActiveDirectoryInteractive): |
| 115 | + if p.applicationClientID == "" { |
| 116 | + return errors.New("applicationclientid parameter is required for " + ActiveDirectoryInteractive) |
| 117 | + } |
| 118 | + p.adalWorkflow = mssql.FedAuthADALWorkflowPassword |
| 119 | + // user is an optional login hint |
| 120 | + p.user, _ = params["user id"] |
| 121 | + // we don't really have a password but we need to use some value. |
| 122 | + p.adalWorkflow = mssql.FedAuthADALWorkflowPassword |
| 123 | + |
| 124 | + default: |
| 125 | + return fmt.Errorf("Invalid federated authentication type '%s': expected one of %+v", |
| 126 | + fedAuthWorkflow, |
| 127 | + []string{ActiveDirectoryApplication, ActiveDirectoryServicePrincipal, ActiveDirectoryDefault, ActiveDirectoryIntegrated, ActiveDirectoryInteractive, ActiveDirectoryManagedIdentity, ActiveDirectoryMSI, ActiveDirectoryPassword}) |
| 128 | + } |
| 129 | + p.fedAuthWorkflow = fedAuthWorkflow |
| 130 | + return nil |
| 131 | +} |
| 132 | + |
| 133 | +func splitTenantAndClientID(user string) (string, string) { |
| 134 | + // Split the user name into client id and tenant id at the @ symbol |
| 135 | + at := strings.IndexRune(user, '@') |
| 136 | + if at < 1 || at >= (len(user)-1) { |
| 137 | + return user, "" |
| 138 | + } |
| 139 | + |
| 140 | + return user[0:at], user[at+1:] |
| 141 | +} |
| 142 | + |
| 143 | +func splitAuthorityAndTenant(authorityUrl string) (string, string) { |
| 144 | + separatorIndex := strings.LastIndex(authorityUrl, "/") |
| 145 | + tenant := authorityUrl[separatorIndex+1:] |
| 146 | + authority := authorityUrl[:separatorIndex] |
| 147 | + return authority, tenant |
| 148 | +} |
| 149 | + |
| 150 | +func (p *azureFedAuthConfig) provideActiveDirectoryToken(ctx context.Context, serverSPN, stsURL string) (string, error) { |
| 151 | + var cred azcore.TokenCredential |
| 152 | + var err error |
| 153 | + authority, tenant := splitAuthorityAndTenant(stsURL) |
| 154 | + // client secret connection strings may override the server tenant |
| 155 | + if p.tenantID != "" { |
| 156 | + tenant = p.tenantID |
| 157 | + } |
| 158 | + scope := stsURL |
| 159 | + if !strings.HasSuffix(serverSPN, scopeDefaultSuffix) { |
| 160 | + scope = strings.TrimRight(serverSPN, "/") + scopeDefaultSuffix |
| 161 | + } |
| 162 | + |
| 163 | + switch p.fedAuthWorkflow { |
| 164 | + case ActiveDirectoryServicePrincipal, ActiveDirectoryApplication: |
| 165 | + switch { |
| 166 | + case p.certificatePath != "": |
| 167 | + cred, err = azidentity.NewClientCertificateCredential(tenant, p.clientID, p.certificatePath, &azidentity.ClientCertificateCredentialOptions{Password: p.clientSecret}) |
| 168 | + default: |
| 169 | + cred, err = azidentity.NewClientSecretCredential(tenant, p.clientID, p.clientSecret, nil) |
| 170 | + } |
| 171 | + case ActiveDirectoryPassword: |
| 172 | + cred, err = azidentity.NewUsernamePasswordCredential(tenant, p.applicationClientID, p.user, p.password, nil) |
| 173 | + case ActiveDirectoryMSI, ActiveDirectoryManagedIdentity: |
| 174 | + cred, err = azidentity.NewManagedIdentityCredential(p.clientID, nil) |
| 175 | + case ActiveDirectoryInteractive: |
| 176 | + cred, err = azidentity.NewInteractiveBrowserCredential(&azidentity.InteractiveBrowserCredentialOptions{AuthorityHost: authority, ClientID: p.applicationClientID}) |
| 177 | + |
| 178 | + default: |
| 179 | + // Integrated just uses Default until azidentity adds Windows-specific authentication |
| 180 | + cred, err = azidentity.NewDefaultAzureCredential(nil) |
| 181 | + } |
| 182 | + |
| 183 | + if err != nil { |
| 184 | + return "", err |
| 185 | + } |
| 186 | + opts := policy.TokenRequestOptions{Scopes: []string{scope}} |
| 187 | + tk, err := cred.GetToken(ctx, opts) |
| 188 | + if err != nil { |
| 189 | + return "", err |
| 190 | + } |
| 191 | + return tk.Token, err |
| 192 | +} |
0 commit comments