Skip to content

Commit

Permalink
Merge pull request #103 from DopplerHQ/tom_config_info
Browse files Browse the repository at this point in the history
Add support for locking/unlocking a config
  • Loading branch information
Piccirello authored Jun 29, 2020
2 parents 855eb58 + 51b6d94 commit 96b429a
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 3 deletions.
74 changes: 74 additions & 0 deletions pkg/cmd/enclave_configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,70 @@ var configsUpdateCmd = &cobra.Command{
},
}

var configsLockCmd = &cobra.Command{
Use: "lock [config]",
Short: "Lock a config",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
jsonFlag := utils.OutputJSON
silent := utils.GetBoolFlag(cmd, "silent")
yes := utils.GetBoolFlag(cmd, "yes")
localConfig := configuration.LocalConfig(cmd)

utils.RequireValue("token", localConfig.Token.Value)
utils.RequireValue("project", localConfig.EnclaveProject.Value)

config := localConfig.EnclaveConfig.Value
if len(args) > 0 {
config = args[0]
}
utils.RequireValue("config", config)

if yes || utils.ConfirmationPrompt("Lock config "+config, false) {
configInfo, err := http.LockConfig(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, localConfig.EnclaveProject.Value, config)
if !err.IsNil() {
utils.HandleError(err.Unwrap(), err.Message)
}

if !silent {
printer.ConfigInfo(configInfo, jsonFlag)
}
}
},
}

var configsUnlockCmd = &cobra.Command{
Use: "unlock [config]",
Short: "Unlock a config",
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
jsonFlag := utils.OutputJSON
silent := utils.GetBoolFlag(cmd, "silent")
yes := utils.GetBoolFlag(cmd, "yes")
localConfig := configuration.LocalConfig(cmd)

utils.RequireValue("token", localConfig.Token.Value)
utils.RequireValue("project", localConfig.EnclaveProject.Value)

config := localConfig.EnclaveConfig.Value
if len(args) > 0 {
config = args[0]
}
utils.RequireValue("config", config)

if yes || utils.ConfirmationPrompt("Unlock config "+config, false) {
configInfo, err := http.UnlockConfig(localConfig.APIHost.Value, utils.GetBool(localConfig.VerifyTLS.Value, true), localConfig.Token.Value, localConfig.EnclaveProject.Value, config)
if !err.IsNil() {
utils.HandleError(err.Unwrap(), err.Message)
}

if !silent {
printer.ConfigInfo(configInfo, jsonFlag)
}
}
},
}

func init() {
configsCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")

Expand All @@ -206,5 +270,15 @@ func init() {
configsDeleteCmd.Flags().Bool("yes", false, "proceed without confirmation")
configsCmd.AddCommand(configsDeleteCmd)

configsLockCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
configsLockCmd.Flags().StringP("config", "c", "", "enclave config (e.g. dev)")
configsLockCmd.Flags().Bool("yes", false, "proceed without confirmation")
configsCmd.AddCommand(configsLockCmd)

configsUnlockCmd.Flags().StringP("project", "p", "", "enclave project (e.g. backend)")
configsUnlockCmd.Flags().StringP("config", "c", "", "enclave config (e.g. dev)")
configsUnlockCmd.Flags().Bool("yes", false, "proceed without confirmation")
configsCmd.AddCommand(configsUnlockCmd)

enclaveCmd.AddCommand(configsCmd)
}
46 changes: 43 additions & 3 deletions pkg/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ func CreateConfig(host string, verifyTLS bool, apiKey string, project string, na
return info, Error{}
}

// DeleteConfig create a config
// DeleteConfig delete a config
func DeleteConfig(host string, verifyTLS bool, apiKey string, project string, config string) Error {
var params []queryParam
params = append(params, queryParam{Key: "project", Value: project})
Expand All @@ -472,7 +472,47 @@ func DeleteConfig(host string, verifyTLS bool, apiKey string, project string, co
return Error{}
}

// UpdateConfig create a config
// LockConfig lock a config
func LockConfig(host string, verifyTLS bool, apiKey string, project string, config string) (models.ConfigInfo, Error) {
var params []queryParam
params = append(params, queryParam{Key: "project", Value: project})

statusCode, response, err := PostRequest(host, verifyTLS, apiKeyHeader(apiKey), "/enclave/v1/configs/"+config+"/lock", params, nil)
if err != nil {
return models.ConfigInfo{}, Error{Err: err, Message: "Unable to lock config", Code: statusCode}
}

var result map[string]interface{}
err = json.Unmarshal(response, &result)
if err != nil {
return models.ConfigInfo{}, Error{Err: err, Message: "Unable to parse API response", Code: statusCode}
}

info := models.ParseConfigInfo(result["config"].(map[string]interface{}))
return info, Error{}
}

// UnlockConfig unlock a config
func UnlockConfig(host string, verifyTLS bool, apiKey string, project string, config string) (models.ConfigInfo, Error) {
var params []queryParam
params = append(params, queryParam{Key: "project", Value: project})

statusCode, response, err := PostRequest(host, verifyTLS, apiKeyHeader(apiKey), "/enclave/v1/configs/"+config+"/unlock", params, nil)
if err != nil {
return models.ConfigInfo{}, Error{Err: err, Message: "Unable to unlock config", Code: statusCode}
}

var result map[string]interface{}
err = json.Unmarshal(response, &result)
if err != nil {
return models.ConfigInfo{}, Error{Err: err, Message: "Unable to parse API response", Code: statusCode}
}

info := models.ParseConfigInfo(result["config"].(map[string]interface{}))
return info, Error{}
}

// UpdateConfig update a config
func UpdateConfig(host string, verifyTLS bool, apiKey string, project string, config string, name string) (models.ConfigInfo, Error) {
postBody := map[string]interface{}{"name": name}
body, err := json.Marshal(postBody)
Expand Down Expand Up @@ -585,7 +625,7 @@ func RollbackConfigLog(host string, verifyTLS bool, apiKey string, project strin
var params []queryParam
params = append(params, queryParam{Key: "project", Value: project})

statusCode, response, err := PostRequest(host, verifyTLS, apiKeyHeader(apiKey), "/enclave/v1/configs/"+config+"/logs/"+log+"/rollback", params, []byte{})
statusCode, response, err := PostRequest(host, verifyTLS, apiKeyHeader(apiKey), "/enclave/v1/configs/"+config+"/logs/"+log+"/rollback", params, nil)
if err != nil {
return models.ConfigLog{}, Error{Err: err, Message: "Unable to rollback config log", Code: statusCode}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/models/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ type EnvironmentInfo struct {
// ConfigInfo project info
type ConfigInfo struct {
Name string `json:"name"`
Root bool `json:"root"`
Locked bool `json:"locked"`
Environment string `json:"environment"`
Project string `json:"project"`
CreatedAt string `json:"created_at"`
Expand Down
6 changes: 6 additions & 0 deletions pkg/models/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func ParseConfigInfo(info map[string]interface{}) ConfigInfo {
if info["name"] != nil {
configInfo.Name = info["name"].(string)
}
if info["root"] != nil {
configInfo.Root = info["root"].(bool)
}
if info["locked"] != nil {
configInfo.Locked = info["locked"].(bool)
}
if info["environment"] != nil {
configInfo.Environment = info["environment"].(string)
}
Expand Down

0 comments on commit 96b429a

Please sign in to comment.