chain.go (2906B)
1 /* 2 * MinIO Go Library for Amazon S3 Compatible Cloud Storage 3 * Copyright 2017 MinIO, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package credentials 19 20 // A Chain will search for a provider which returns credentials 21 // and cache that provider until Retrieve is called again. 22 // 23 // The Chain provides a way of chaining multiple providers together 24 // which will pick the first available using priority order of the 25 // Providers in the list. 26 // 27 // If none of the Providers retrieve valid credentials Value, ChainProvider's 28 // Retrieve() will return the no credentials value. 29 // 30 // If a Provider is found which returns valid credentials Value ChainProvider 31 // will cache that Provider for all calls to IsExpired(), until Retrieve is 32 // called again after IsExpired() is true. 33 // 34 // creds := credentials.NewChainCredentials( 35 // []credentials.Provider{ 36 // &credentials.EnvAWSS3{}, 37 // &credentials.EnvMinio{}, 38 // }) 39 // 40 // // Usage of ChainCredentials. 41 // mc, err := minio.NewWithCredentials(endpoint, creds, secure, "us-east-1") 42 // if err != nil { 43 // log.Fatalln(err) 44 // } 45 type Chain struct { 46 Providers []Provider 47 curr Provider 48 } 49 50 // NewChainCredentials returns a pointer to a new Credentials object 51 // wrapping a chain of providers. 52 func NewChainCredentials(providers []Provider) *Credentials { 53 return New(&Chain{ 54 Providers: append([]Provider{}, providers...), 55 }) 56 } 57 58 // Retrieve returns the credentials value, returns no credentials(anonymous) 59 // if no credentials provider returned any value. 60 // 61 // If a provider is found with credentials, it will be cached and any calls 62 // to IsExpired() will return the expired state of the cached provider. 63 func (c *Chain) Retrieve() (Value, error) { 64 for _, p := range c.Providers { 65 creds, _ := p.Retrieve() 66 // Always prioritize non-anonymous providers, if any. 67 if creds.AccessKeyID == "" && creds.SecretAccessKey == "" { 68 continue 69 } 70 c.curr = p 71 return creds, nil 72 } 73 // At this point we have exhausted all the providers and 74 // are left without any credentials return anonymous. 75 return Value{ 76 SignerType: SignatureAnonymous, 77 }, nil 78 } 79 80 // IsExpired will returned the expired state of the currently cached provider 81 // if there is one. If there is no current provider, true will be returned. 82 func (c *Chain) IsExpired() bool { 83 if c.curr != nil { 84 return c.curr.IsExpired() 85 } 86 87 return true 88 }