argo-cd/util/cli/config.go
Andrew Merenbach 685a814f38
Add argocd login command (#82)
* Add auth check and bypass for authentication

* Disallow blank passwords

* Mitigate timing attacks

* Factor out authentication/token gen code

* Tweaked token validation code to log claims

* Add missing internal gRPC client endpoints

* Add first draft of login command

* Add login command to root commands

* Get login working

* Generalize command utils for unmarshaling

* Centralize utils for CLI YAML/JSON parsing

* Read/write local config now

* Initialize map

* Revert server files for now

* Fix casing

* Restore commented test, thanks @alexmt

* No need to mitigate timing attacks on blank passwords, thanks @alexmt

* Rm redundant type declaration, thanks @alexmt

* Improve error checks

* Rm unnecessary conversion, thanks @alexmt

* Fix comment

* Don't return error when config doesn't exist
2018-04-11 17:01:58 -07:00

74 lines
2.1 KiB
Go

package cli
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/ghodss/yaml"
)
// unmarshalObject tries to convert a YAML or JSON byte array into the provided type.
func unmarshalObject(data []byte, obj interface{}) error {
// first, try unmarshaling as JSON
// Based on technique from Kubectl, which supports both YAML and JSON:
// https://mlafeldt.github.io/blog/teaching-go-programs-to-love-json-and-yaml/
// http://ghodss.com/2014/the-right-way-to-handle-yaml-in-golang/
// Short version: JSON unmarshaling won't zero out null fields; YAML unmarshaling will.
// This may have unintended effects or hard-to-catch issues when populating our application object.
jsonData, err := yaml.YAMLToJSON(data)
if err != nil {
return err
}
err = json.Unmarshal(jsonData, &obj)
if err != nil {
return err
}
return err
}
// MarshalLocalYAMLFile writes JSON or YAML to a file on disk.
// The caller is responsible for checking error return values.
func MarshalLocalYAMLFile(path string, obj interface{}) error {
yamlData, err := yaml.Marshal(obj)
if err == nil {
err = ioutil.WriteFile(path, yamlData, 0600)
}
return err
}
// UnmarshalLocalFile retrieves JSON or YAML from a file on disk.
// The caller is responsible for checking error return values.
func UnmarshalLocalFile(path string, obj interface{}) error {
data, err := ioutil.ReadFile(path)
if err == nil {
err = unmarshalObject(data, obj)
}
return err
}
// UnmarshalRemoteFile retrieves JSON or YAML through a GET request.
// The caller is responsible for checking error return values.
func UnmarshalRemoteFile(url string, obj interface{}) error {
data, err := readRemoteFile(url)
if err == nil {
err = unmarshalObject(data, obj)
}
return err
}
// ReadRemoteFile issues a GET request to retrieve the contents of the specified URL as a byte array.
// The caller is responsible for checking error return values.
func readRemoteFile(url string) ([]byte, error) {
var data []byte
resp, err := http.Get(url)
if err == nil {
defer func() {
_ = resp.Body.Close()
}()
data, err = ioutil.ReadAll(resp.Body)
}
return data, err
}