| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package open |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
| 22 | "io" |
| 23 | |
| 24 | "github.com/sassoftware/relic/v7/config" |
| 25 | "github.com/sassoftware/relic/v7/lib/passprompt" |
| 26 | "github.com/sassoftware/relic/v7/token" |
| 27 | |
| 28 | // Token types that don't require cgo |
| 29 | _ "github.com/sassoftware/relic/v7/token/awstoken" |
| 30 | _ "github.com/sassoftware/relic/v7/token/azuretoken" |
| 31 | _ "github.com/sassoftware/relic/v7/token/filetoken" |
| 32 | _ "github.com/sassoftware/relic/v7/token/gcloudtoken" |
| 33 | _ "github.com/sassoftware/relic/v7/token/scdtoken" |
| 34 | ) |
| 35 | |
| 36 | func Token(cfg *config.Config, tokenName string, prompt passprompt.PasswordGetter) (token.Token, error) { |
| 37 | tcfg, err := cfg.GetToken(tokenName) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | if ofunc := token.Openers[tcfg.Type]; ofunc != nil { |
| 42 | return ofunc(cfg, tokenName, prompt) |
| 43 | } |
| 44 | var msg string |
| 45 | if tcfg.Type == "pkcs11" { |
| 46 | msg = " -- built without pkcs11 support" |
| 47 | } |
| 48 | return nil, fmt.Errorf("unknown token type %s%s", tcfg.Type, msg) |
| 49 | } |
| 50 | |
| 51 | func Key(cfg *config.Config, keyName string, prompt passprompt.PasswordGetter) (token.Key, error) { |
| 52 | keyConf, err := cfg.GetKey(keyName) |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | token, err := Token(cfg, keyConf.Token, prompt) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | key, err := token.GetKey(context.Background(), keyName) |
| 61 | if err != nil { |
| 62 | token.Close() |
| 63 | return nil, err |
| 64 | } |
| 65 | return key, nil |
| 66 | } |
| 67 | |
| 68 | func List(tokenType, provider string, w io.Writer) error { |
| 69 | if listFunc := token.Listers[tokenType]; listFunc != nil { |
| 70 | return listFunc(provider, w) |
| 71 | } |
| 72 | if token.Openers[tokenType] != nil { |
| 73 | return fmt.Errorf("list operation not supported for token type %s", tokenType) |
| 74 | } |
| 75 | var msg string |
| 76 | if tokenType == "pkcs11" { |
| 77 | msg = " -- built without pkcs11 support" |
| 78 | } |
| 79 | return fmt.Errorf("unknown token type %s%s", tokenType, msg) |
| 80 | } |