Swift: introduce application credential auth support

This commit is contained in:
kayrus 2019-01-28 17:55:17 +01:00 committed by Nick Craig-Wood
parent 38c0018906
commit 34baf05d9d
3 changed files with 83 additions and 39 deletions

View File

@ -67,7 +67,7 @@ ifdef FULL_TESTS
go vet $(BUILDTAGS) -printfuncs Debugf,Infof,Logf,Errorf ./... go vet $(BUILDTAGS) -printfuncs Debugf,Infof,Logf,Errorf ./...
errcheck $(BUILDTAGS) ./... errcheck $(BUILDTAGS) ./...
find . -name \*.go | grep -v /vendor/ | xargs goimports -d | grep . ; test $$? -eq 1 find . -name \*.go | grep -v /vendor/ | xargs goimports -d | grep . ; test $$? -eq 1
go list ./... | xargs -n1 golint | grep -E -v '(StorageUrl|CdnUrl)' ; test $$? -eq 1 go list ./... | xargs -n1 golint | grep -E -v '(StorageUrl|CdnUrl|ApplicationCredentialId)' ; test $$? -eq 1
else else
@echo Skipping source quality tests as version of go too old @echo Skipping source quality tests as version of go too old
endif endif

View File

@ -130,6 +130,15 @@ func init() {
}, { }, {
Name: "auth_token", Name: "auth_token",
Help: "Auth Token from alternate authentication - optional (OS_AUTH_TOKEN)", Help: "Auth Token from alternate authentication - optional (OS_AUTH_TOKEN)",
}, {
Name: "application_credential_id",
Help: "Application Credential ID (OS_APPLICATION_CREDENTIAL_ID)",
}, {
Name: "application_credential_name",
Help: "Application Credential Name (OS_APPLICATION_CREDENTIAL_NAME)",
}, {
Name: "application_credential_secret",
Help: "Application Credential Secret (OS_APPLICATION_CREDENTIAL_SECRET)",
}, { }, {
Name: "auth_version", Name: "auth_version",
Help: "AuthVersion - optional - set to (1,2,3) if your auth URL has no version (ST_AUTH_VERSION)", Help: "AuthVersion - optional - set to (1,2,3) if your auth URL has no version (ST_AUTH_VERSION)",
@ -173,23 +182,26 @@ provider.`,
// Options defines the configuration for this backend // Options defines the configuration for this backend
type Options struct { type Options struct {
EnvAuth bool `config:"env_auth"` EnvAuth bool `config:"env_auth"`
User string `config:"user"` User string `config:"user"`
Key string `config:"key"` Key string `config:"key"`
Auth string `config:"auth"` Auth string `config:"auth"`
UserID string `config:"user_id"` UserID string `config:"user_id"`
Domain string `config:"domain"` Domain string `config:"domain"`
Tenant string `config:"tenant"` Tenant string `config:"tenant"`
TenantID string `config:"tenant_id"` TenantID string `config:"tenant_id"`
TenantDomain string `config:"tenant_domain"` TenantDomain string `config:"tenant_domain"`
Region string `config:"region"` Region string `config:"region"`
StorageURL string `config:"storage_url"` StorageURL string `config:"storage_url"`
AuthToken string `config:"auth_token"` AuthToken string `config:"auth_token"`
AuthVersion int `config:"auth_version"` AuthVersion int `config:"auth_version"`
StoragePolicy string `config:"storage_policy"` ApplicationCredentialId string `config:"application_credential_id"`
EndpointType string `config:"endpoint_type"` ApplicationCredentialName string `config:"application_credential_name"`
ChunkSize fs.SizeSuffix `config:"chunk_size"` ApplicationCredentialSecret string `config:"application_credential_secret"`
NoChunk bool `config:"no_chunk"` StoragePolicy string `config:"storage_policy"`
EndpointType string `config:"endpoint_type"`
ChunkSize fs.SizeSuffix `config:"chunk_size"`
NoChunk bool `config:"no_chunk"`
} }
// Fs represents a remote swift server // Fs represents a remote swift server
@ -293,22 +305,25 @@ func parsePath(path string) (container, directory string, err error) {
func swiftConnection(opt *Options, name string) (*swift.Connection, error) { func swiftConnection(opt *Options, name string) (*swift.Connection, error) {
c := &swift.Connection{ c := &swift.Connection{
// Keep these in the same order as the Config for ease of checking // Keep these in the same order as the Config for ease of checking
UserName: opt.User, UserName: opt.User,
ApiKey: opt.Key, ApiKey: opt.Key,
AuthUrl: opt.Auth, AuthUrl: opt.Auth,
UserId: opt.UserID, UserId: opt.UserID,
Domain: opt.Domain, Domain: opt.Domain,
Tenant: opt.Tenant, Tenant: opt.Tenant,
TenantId: opt.TenantID, TenantId: opt.TenantID,
TenantDomain: opt.TenantDomain, TenantDomain: opt.TenantDomain,
Region: opt.Region, Region: opt.Region,
StorageUrl: opt.StorageURL, StorageUrl: opt.StorageURL,
AuthToken: opt.AuthToken, AuthToken: opt.AuthToken,
AuthVersion: opt.AuthVersion, AuthVersion: opt.AuthVersion,
EndpointType: swift.EndpointType(opt.EndpointType), ApplicationCredentialId: opt.ApplicationCredentialId,
ConnectTimeout: 10 * fs.Config.ConnectTimeout, // Use the timeouts in the transport ApplicationCredentialName: opt.ApplicationCredentialName,
Timeout: 10 * fs.Config.Timeout, // Use the timeouts in the transport ApplicationCredentialSecret: opt.ApplicationCredentialSecret,
Transport: fshttp.NewTransport(fs.Config), EndpointType: swift.EndpointType(opt.EndpointType),
ConnectTimeout: 10 * fs.Config.ConnectTimeout, // Use the timeouts in the transport
Timeout: 10 * fs.Config.Timeout, // Use the timeouts in the transport
Transport: fshttp.NewTransport(fs.Config),
} }
if opt.EnvAuth { if opt.EnvAuth {
err := c.ApplyEnvironment() err := c.ApplyEnvironment()
@ -318,11 +333,13 @@ func swiftConnection(opt *Options, name string) (*swift.Connection, error) {
} }
StorageUrl, AuthToken := c.StorageUrl, c.AuthToken // nolint StorageUrl, AuthToken := c.StorageUrl, c.AuthToken // nolint
if !c.Authenticated() { if !c.Authenticated() {
if c.UserName == "" && c.UserId == "" { if (c.ApplicationCredentialId != "" || c.ApplicationCredentialName != "") && c.ApplicationCredentialSecret == "" {
return nil, errors.New("user name or user id not found for authentication (and no storage_url+auth_token is provided)") if c.UserName == "" && c.UserId == "" {
} return nil, errors.New("user name or user id not found for authentication (and no storage_url+auth_token is provided)")
if c.ApiKey == "" { }
return nil, errors.New("key not found") if c.ApiKey == "" {
return nil, errors.New("key not found")
}
} }
if c.AuthUrl == "" { if c.AuthUrl == "" {
return nil, errors.New("auth not found") return nil, errors.New("auth not found")

View File

@ -329,6 +329,33 @@ User ID to log in - optional - most swift systems use user and leave this blank
- Type: string - Type: string
- Default: "" - Default: ""
#### --swift-application-credential-id
Application Credential ID to log in - optional (v3 auth) (OS_APPLICATION_CREDENTIAL_ID).
- Config: application_credential_id
- Env Var: RCLONE_SWIFT_APPLICATION_CREDENTIAL_ID
- Type: string
- Default: ""
#### --swift-application-credential-name
Application Credential name to log in - optional (v3 auth) (OS_APPLICATION_CREDENTIAL_NAME).
- Config: application_credential_name
- Env Var: RCLONE_SWIFT_APPLICATION_CREDENTIAL_NAME
- Type: string
- Default: ""
#### --swift-application-credential-secret
Application Credential secret to log in - optional (v3 auth) (OS_APPLICATION_CREDENTIAL_SECRET).
- Config: application_credential_secret
- Env Var: RCLONE_SWIFT_APPLICATION_CREDENTIAL_SECRET
- Type: string
- Default: ""
#### --swift-domain #### --swift-domain
User domain - optional (v3 auth) (OS_USER_DOMAIN_NAME) User domain - optional (v3 auth) (OS_USER_DOMAIN_NAME)