|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2019 Dell Inc. |
| 3 | + * Copyright 2021 Intel Corp. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. 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 distributed under the License |
| 11 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 12 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 13 | + * the License. |
| 14 | + *******************************************************************************/ |
| 15 | + |
| 16 | +package vault |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "crypto/tls" |
| 21 | + "crypto/x509" |
| 22 | + "io/ioutil" |
| 23 | + "net/http" |
| 24 | + "time" |
| 25 | + |
| 26 | + "github.com/edgexfoundry/go-mod-secrets/v2/pkg" |
| 27 | + "github.com/edgexfoundry/go-mod-secrets/v2/pkg/types" |
| 28 | + |
| 29 | + "github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger" |
| 30 | +) |
| 31 | + |
| 32 | +// *Client defines the behavior for interacting with the Vault REST secret key/value store via HTTP(S). |
| 33 | +type Client struct { |
| 34 | + Config types.SecretConfig |
| 35 | + HttpCaller pkg.Caller |
| 36 | + lc logger.LoggingClient |
| 37 | + context context.Context |
| 38 | +} |
| 39 | + |
| 40 | +// NewVaultClient constructs a Vault *Client which communicates with Vault via HTTP(S) |
| 41 | +// |
| 42 | +// lc is any logging client that implements the loggingClient interface; |
| 43 | +// today EdgeX's logger.LoggingClient from go-mod-core-contracts satisfies this implementation |
| 44 | +// |
| 45 | +func NewClient(config types.SecretConfig, requester pkg.Caller, forSecrets bool, lc logger.LoggingClient) (*Client, error) { |
| 46 | + if forSecrets && config.Authentication.AuthToken == "" { |
| 47 | + return nil, pkg.NewErrSecretStore("AuthToken is required in config") |
| 48 | + } |
| 49 | + |
| 50 | + var err error |
| 51 | + if requester == nil { |
| 52 | + requester, err = createHTTPClient(config) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + if config.RetryWaitPeriod != "" { |
| 59 | + retryTimeDuration, err := time.ParseDuration(config.RetryWaitPeriod) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + config.RetryWaitPeriodTime = retryTimeDuration |
| 64 | + } |
| 65 | + |
| 66 | + vaultClient := Client{ |
| 67 | + Config: config, |
| 68 | + HttpCaller: requester, |
| 69 | + lc: lc, |
| 70 | + } |
| 71 | + |
| 72 | + return &vaultClient, err |
| 73 | +} |
| 74 | + |
| 75 | +func createHTTPClient(config types.SecretConfig) (pkg.Caller, error) { |
| 76 | + |
| 77 | + if config.RootCaCertPath == "" { |
| 78 | + return http.DefaultClient, nil |
| 79 | + } |
| 80 | + |
| 81 | + // Read and load the CA Root certificate so the client will be able to use TLS without skipping the verification of |
| 82 | + // the cert received by the server. |
| 83 | + caCert, err := ioutil.ReadFile(config.RootCaCertPath) |
| 84 | + if err != nil { |
| 85 | + return nil, ErrCaRootCert{ |
| 86 | + path: config.RootCaCertPath, |
| 87 | + description: err.Error(), |
| 88 | + } |
| 89 | + } |
| 90 | + caCertPool := x509.NewCertPool() |
| 91 | + caCertPool.AppendCertsFromPEM(caCert) |
| 92 | + |
| 93 | + return &http.Client{ |
| 94 | + Transport: &http.Transport{ |
| 95 | + TLSClientConfig: &tls.Config{ |
| 96 | + RootCAs: caCertPool, |
| 97 | + ServerName: config.ServerName, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, nil |
| 101 | +} |
0 commit comments