Skip to content

Commit

Permalink
Rename config structs
Browse files Browse the repository at this point in the history
- Config to UserConfig
- FlagNames to Config
- Use AtlantisVersion to be clear
  • Loading branch information
psalaberria002 committed Mar 11, 2018
1 parent d8f5ff7 commit 1154a09
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 87 deletions.
65 changes: 33 additions & 32 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// To add a new flag you must:
// 1. Add a const with the flag name (in alphabetic order).
// 2. Add a new field to server.Config and set the mapstructure tag equal to the flag name.
// 2. Add a new field to server.UserConfig and set the mapstructure tag equal to the flag name.
// 3. Add your flag's description etc. to the stringFlags, intFlags, or boolFlags slices.
const (
AtlantisURLFlag = "atlantis-url"
Expand Down Expand Up @@ -169,7 +169,7 @@ type ServerCmd struct {
// ServerCreator creates servers.
// It's an abstraction to help us test.
type ServerCreator interface {
NewServer(config server.Config, flagNames server.FlagNames) (ServerStarter, error)
NewServer(userConfig server.UserConfig, config server.Config) (ServerStarter, error)
}

// DefaultServerCreator is the concrete implementation of ServerCreator.
Expand All @@ -182,8 +182,8 @@ type ServerStarter interface {
}

// NewServer returns the real Atlantis server object.
func (d *DefaultServerCreator) NewServer(config server.Config, flagNames server.FlagNames) (ServerStarter, error) {
return server.NewServer(config, flagNames)
func (d *DefaultServerCreator) NewServer(userConfig server.UserConfig, config server.Config) (ServerStarter, error) {
return server.NewServer(userConfig, config)
}

// Init returns the runnable cobra command.
Expand All @@ -194,7 +194,7 @@ func (s *ServerCmd) Init() *cobra.Command {
Long: `Start the atlantis server
Flags can also be set in a yaml config file (see --` + ConfigFlag + `).
Config file values are overridden by environment variables which in turn are overridden by flags.`,
UserConfig file values are overridden by environment variables which in turn are overridden by flags.`,
SilenceErrors: true,
SilenceUsage: s.SilenceOutput,
PreRunE: s.withErrPrint(func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -252,25 +252,26 @@ func (s *ServerCmd) preRun() error {
}

func (s *ServerCmd) run() error {
var config server.Config
if err := s.Viper.Unmarshal(&config); err != nil {
var userConfig server.UserConfig
if err := s.Viper.Unmarshal(&userConfig); err != nil {
return err
}
if err := s.validate(config); err != nil {
if err := s.validate(userConfig); err != nil {
return err
}
if err := s.setAtlantisURL(&config); err != nil {
if err := s.setAtlantisURL(&userConfig); err != nil {
return err
}
if err := s.setDataDir(&config); err != nil {
if err := s.setDataDir(&userConfig); err != nil {
return err
}
s.securityWarnings(&config)
s.trimAtSymbolFromUsers(&config)
s.securityWarnings(&userConfig)
s.trimAtSymbolFromUsers(&userConfig)

// Config looks good. Start the server.
server, err := s.ServerCreator.NewServer(config, server.FlagNames{
// UserConfig looks good. Start the server.
server, err := s.ServerCreator.NewServer(userConfig, server.Config{
AllowForkPRsFlag: AllowForkPRsFlag,
AtlantisVersion: s.Viper.Get("version").(string),
})
if err != nil {
return errors.Wrap(err, "initializing server")
Expand All @@ -279,13 +280,13 @@ func (s *ServerCmd) run() error {
}

// nolint: gocyclo
func (s *ServerCmd) validate(config server.Config) error {
logLevel := config.LogLevel
func (s *ServerCmd) validate(userConfig server.UserConfig) error {
logLevel := userConfig.LogLevel
if logLevel != "debug" && logLevel != "info" && logLevel != "warn" && logLevel != "error" {
return errors.New("invalid log level: not one of debug, info, warn, error")
}

if (config.SSLKeyFile == "") != (config.SSLCertFile == "") {
if (userConfig.SSLKeyFile == "") != (userConfig.SSLCertFile == "") {
return fmt.Errorf("--%s and --%s are both required for ssl", SSLKeyFileFlag, SSLCertFileFlag)
}

Expand All @@ -294,39 +295,39 @@ func (s *ServerCmd) validate(config server.Config) error {
// 2. gitlab user and token set
// 3. all 4 set
vcsErr := fmt.Errorf("--%s and --%s or --%s and --%s must be set", GHUserFlag, GHTokenFlag, GitlabUserFlag, GitlabTokenFlag)
if ((config.GithubUser == "") != (config.GithubToken == "")) || ((config.GitlabUser == "") != (config.GitlabToken == "")) {
if ((userConfig.GithubUser == "") != (userConfig.GithubToken == "")) || ((userConfig.GitlabUser == "") != (userConfig.GitlabToken == "")) {
return vcsErr
}
// At this point, we know that there can't be a single user/token without
// its partner, but we haven't checked if any user/token is set at all.
if config.GithubUser == "" && config.GitlabUser == "" {
if userConfig.GithubUser == "" && userConfig.GitlabUser == "" {
return vcsErr
}

if config.RepoWhitelist == "" {
if userConfig.RepoWhitelist == "" {
return fmt.Errorf("--%s must be set for security purposes", RepoWhitelistFlag)
}

return nil
}

// setAtlantisURL sets the externally accessible URL for atlantis.
func (s *ServerCmd) setAtlantisURL(config *server.Config) error {
if config.AtlantisURL == "" {
func (s *ServerCmd) setAtlantisURL(userConfig *server.UserConfig) error {
if userConfig.AtlantisURL == "" {
hostname, err := os.Hostname()
if err != nil {
return fmt.Errorf("Failed to determine hostname: %v", err)
}
config.AtlantisURL = fmt.Sprintf("http://%s:%d", hostname, config.Port)
userConfig.AtlantisURL = fmt.Sprintf("http://%s:%d", hostname, userConfig.Port)
}
return nil
}

// setDataDir checks if ~ was used in data-dir and converts it to the actual
// home directory. If we don't do this, we'll create a directory called "~"
// instead of actually using home. It also converts relative paths to absolute.
func (s *ServerCmd) setDataDir(config *server.Config) error {
finalPath := config.DataDir
func (s *ServerCmd) setDataDir(userConfig *server.UserConfig) error {
finalPath := userConfig.DataDir

// Convert ~ to the actual home dir.
if strings.HasPrefix(finalPath, "~/") {
Expand All @@ -342,21 +343,21 @@ func (s *ServerCmd) setDataDir(config *server.Config) error {
if err != nil {
return errors.Wrap(err, "making data-dir absolute")
}
config.DataDir = finalPath
userConfig.DataDir = finalPath
return nil
}

// trimAtSymbolFromUsers trims @ from the front of the github and gitlab usernames
func (s *ServerCmd) trimAtSymbolFromUsers(config *server.Config) {
config.GithubUser = strings.TrimPrefix(config.GithubUser, "@")
config.GitlabUser = strings.TrimPrefix(config.GitlabUser, "@")
func (s *ServerCmd) trimAtSymbolFromUsers(userConfig *server.UserConfig) {
userConfig.GithubUser = strings.TrimPrefix(userConfig.GithubUser, "@")
userConfig.GitlabUser = strings.TrimPrefix(userConfig.GitlabUser, "@")
}

func (s *ServerCmd) securityWarnings(config *server.Config) {
if config.GithubUser != "" && config.GithubWebHookSecret == "" && !s.SilenceOutput {
func (s *ServerCmd) securityWarnings(userConfig *server.UserConfig) {
if userConfig.GithubUser != "" && userConfig.GithubWebHookSecret == "" && !s.SilenceOutput {
fmt.Fprintf(os.Stderr, "%s[WARN] No GitHub webhook secret set. This could allow attackers to spoof requests from GitHub. See https://git.io/vAF3t%s\n", RedTermStart, RedTermEnd)
}
if config.GitlabUser != "" && config.GitlabWebHookSecret == "" && !s.SilenceOutput {
if userConfig.GitlabUser != "" && userConfig.GitlabWebHookSecret == "" && !s.SilenceOutput {
fmt.Fprintf(os.Stderr, "%s[WARN] No GitLab webhook secret set. This could allow attackers to spoof requests from GitLab. See https://git.io/vAF3t%s\n", RedTermStart, RedTermEnd)
}
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (

// passedConfig is set to whatever config ended up being passed to NewServer.
// Used for testing.
var passedConfig server.Config
var passedConfig server.UserConfig

type ServerCreatorMock struct{}

func (s *ServerCreatorMock) NewServer(config server.Config, flagNames server.FlagNames) (cmd.ServerStarter, error) {
passedConfig = config
func (s *ServerCreatorMock) NewServer(userConfig server.UserConfig, config server.Config) (cmd.ServerStarter, error) {
passedConfig = userConfig
return &ServerStarterMock{}, nil
}

Expand All @@ -47,7 +47,7 @@ func TestExecute_ConfigFileExtension(t *testing.T) {
cmd.ConfigFlag: "does-not-exist",
})
err := c.Execute()
Equals(t, "invalid config: reading does-not-exist: Unsupported Config Type \"\"", err.Error())
Equals(t, "invalid config: reading does-not-exist: Unsupported UserConfig Type \"\"", err.Error())
}

func TestExecute_ConfigFileMissing(t *testing.T) {
Expand Down
Loading

0 comments on commit 1154a09

Please sign in to comment.