Skip to content

Commit

Permalink
Merge pull request #80 from runatlantis/envvars
Browse files Browse the repository at this point in the history
Allow specifying config values as environment variables
  • Loading branch information
lkysow authored Mar 20, 2018
2 parents c7c582c + c8f68e2 commit 9b80b0f
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 71 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,10 @@ If you'd like to test out Atlantis before running it on your own repositories yo
- `atlantis apply` will run `terraform apply`. Since our pull request creates a `null_resource` (which does nothing) this is safe to do.

## Server Configuration
Atlantis configuration can be specified via command line flags or a YAML config file.
The `gh-token` and `gitlab-token` flags can also be specified via the `ATLANTIS_GH_TOKEN` and `ATLANTIS_GITLAB_TOKEN` environment variables respectively.
Configuration for `atlantis server` can be specified via command line flags, environment variables or a YAML config file.
Config file values are overridden by environment variables which in turn are overridden by flags.

### YAML
To use a yaml config file, run atlantis with `--config /path/to/config.yaml`.
The keys of your config file should be the same as the flag, ex.
```yaml
Expand All @@ -454,6 +454,10 @@ gh-token: ...
log-level: ...
```
### Environment Variables
All flags can be specified as environment variables. You need to convert the flag's `-`'s to `_`'s, uppercase all the letters and prefix with `ATLANTIS_`.
For example, `--gh-user` can be set via the environment variable `ATLANTIS_GH_USER`.

To see a list of all flags and their descriptions run `atlantis server --help`

## AWS Credentials
Expand Down
27 changes: 12 additions & 15 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var stringFlags = []stringFlag{
},
{
name: ConfigFlag,
description: "Path to config file.",
description: "Path to config file. All flags can be set in a YAML config file instead.",
},
{
name: DataDirFlag,
Expand All @@ -80,15 +80,13 @@ var stringFlags = []stringFlag{
{
name: GHTokenFlag,
description: "GitHub token of API user. Can also be specified via the ATLANTIS_GH_TOKEN environment variable.",
env: "ATLANTIS_GH_TOKEN",
},
{
name: GHWebHookSecret,
description: "Secret used to validate GitHub webhooks (see https://developer.github.com/webhooks/securing/)." +
" SECURITY WARNING: If not specified, Atlantis won't be able to validate that the incoming webhook call came from GitHub. " +
"This means that an attacker could spoof calls to Atlantis and cause it to perform malicious actions. " +
"Should be specified via the ATLANTIS_GH_WEBHOOK_SECRET environment variable.",
env: "ATLANTIS_GH_WEBHOOK_SECRET",
},
{
name: GitlabHostnameFlag,
Expand All @@ -102,15 +100,13 @@ var stringFlags = []stringFlag{
{
name: GitlabTokenFlag,
description: "GitLab token of API user. Can also be specified via the ATLANTIS_GITLAB_TOKEN environment variable.",
env: "ATLANTIS_GITLAB_TOKEN",
},
{
name: GitlabWebHookSecret,
description: "Optional secret used to validate GitLab webhooks." +
" SECURITY WARNING: If not specified, Atlantis won't be able to validate that the incoming webhook call came from GitLab. " +
"This means that an attacker could spoof calls to Atlantis and cause it to perform malicious actions. " +
"Should be specified via the ATLANTIS_GITLAB_WEBHOOK_SECRET environment variable.",
env: "ATLANTIS_GITLAB_WEBHOOK_SECRET",
},
{
name: LogLevelFlag,
Expand Down Expand Up @@ -156,7 +152,6 @@ type stringFlag struct {
name string
description string
value string
env string
}
type intFlag struct {
name string
Expand Down Expand Up @@ -203,12 +198,9 @@ func (d *DefaultServerCreator) NewServer(userConfig server.UserConfig, config se
// Init returns the runnable cobra command.
func (s *ServerCmd) Init() *cobra.Command {
c := &cobra.Command{
Use: "server",
Short: "Start the atlantis server",
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.`,
Use: "server",
Short: "Start the atlantis server",
Long: `Start the atlantis server and listen for webhook calls.`,
SilenceErrors: true,
SilenceUsage: s.SilenceOutput,
PreRunE: s.withErrPrint(func(cmd *cobra.Command, args []string) error {
Expand All @@ -218,6 +210,14 @@ Config file values are overridden by environment variables which in turn are ove
return s.run()
}),
}

// Configure viper to accept env vars prefixed with ATLANTIS_ that can be
// used instead of flags.
s.Viper.SetEnvPrefix("ATLANTIS")
s.Viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
s.Viper.AutomaticEnv()
s.Viper.SetTypeByDefaultValue(true)

// Replace the call in their template to use the usage function that wraps
// columns to make for a nicer output.
usageWithWrappedCols := strings.Replace(c.UsageTemplate(), ".FlagUsages", ".FlagUsagesWrapped 120", -1)
Expand All @@ -232,9 +232,6 @@ Config file values are overridden by environment variables which in turn are ove
// Set string flags.
for _, f := range stringFlags {
c.Flags().String(f.name, f.value, "> "+f.description)
if f.env != "" {
s.Viper.BindEnv(f.name, f.env) // nolint: errcheck
}
s.Viper.BindPFlag(f.name, c.Flags().Lookup(f.name)) // nolint: errcheck
}

Expand Down
Loading

0 comments on commit 9b80b0f

Please sign in to comment.