Skip to content

Commit

Permalink
Support use of environment variables to configure the client
Browse files Browse the repository at this point in the history
  • Loading branch information
mtharp committed Jul 3, 2018
1 parent c27b690 commit fa6b744
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cmdline/remotecmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type ReaderGetter interface {

// Make a single API request to a named endpoint, handling directory lookup and failover automatically.
func CallRemote(endpoint, method string, query *url.Values, body ReaderGetter) (*http.Response, error) {
if err := shared.InitConfig(); err != nil {
if err := shared.InitClientConfig(); err != nil {
return nil, err
}
if shared.CurrentConfig.Remote == nil {
Expand Down Expand Up @@ -121,7 +121,7 @@ func buildRequest(base, endpoint, method, encoding string, query *url.Values, bo

// Build TLS config based on client configuration
func makeTLSConfig() (*tls.Config, error) {
err := shared.InitConfig()
err := shared.InitClientConfig()
if err != nil {
return nil, err
}
Expand Down
37 changes: 32 additions & 5 deletions cmdline/shared/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,53 @@ import (
)

func InitConfig() error {
return initConfig(false)
}

func InitClientConfig() error {
return initConfig(true)
}

func initConfig(client bool) error {
if CurrentConfig != nil {
return nil
}
dlog.SetLevel(ArgDebug)
usedDefault := false
if ArgConfig == "" {
ArgConfig = config.DefaultConfig()
if ArgConfig == "" {
return errors.New("--config not specified")
}
usedDefault = true
}
config, err := config.ReadFile(ArgConfig)
if client && usedDefault {
cfg, err := config.FromEnvironment()
if err != nil {
return err
} else if cfg != nil {
CurrentConfig = cfg
return nil
}
}
if ArgConfig == "" {
return errors.New("--config not specified")
}
cfg, err := config.ReadFile(ArgConfig)
if err != nil {
if os.IsNotExist(err) && usedDefault {
if client {
// try to use environment
cfg, err = config.FromEnvironment()
if err != nil {
return err
} else if cfg != nil {
CurrentConfig = cfg
return nil
}
}
return fmt.Errorf("--config not specified and default config at %s does not exist", ArgConfig)
}
return err
}
CurrentConfig = config
CurrentConfig = cfg
return nil
}

Expand Down
47 changes: 47 additions & 0 deletions config/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// Copyright (c) SAS Institute Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package config

import (
"errors"
"os"
)

// FromEnvironment tries to build a client-only config from environment variables. If none are set then returns nil.
func FromEnvironment() (*Config, error) {
remoteURL := os.Getenv("RELIC_URL")
if remoteURL == "" {
return nil, nil
}
clientCert := os.Getenv("RELIC_CLIENT_CERT")
clientKey := os.Getenv("RELIC_CLIENT_KEY")
if clientCert == "" {
return nil, errors.New("RELIC_CLIENT_CERT must be set when RELIC_URL is set")
}
if clientKey == "" {
clientKey = clientCert
}
cfg := &Config{
Remote: &RemoteConfig{
DirectoryURL: remoteURL,
KeyFile: clientKey,
CertFile: clientCert,
CaCert: os.Getenv("RELIC_CACERT"),
},
}
return cfg, cfg.Normalize("<env>")
}

0 comments on commit fa6b744

Please sign in to comment.