Skip to content

Commit

Permalink
feat: Implement environment variables for credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
achetronic committed Sep 15, 2023
1 parent 07805c2 commit 646277c
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ They are described in the following table:
| `--config` | Define the path to the config file | `autoheater.yaml` | `--config ./autoheater.yaml` |
| `--log-level` | Define the verbosity of the logs | `info` | `--log-level info` |

## Environment variables

Some parameters can be defined not only by fixing them into the configuration file, but setting them as environment
variables. This is mostly used by credentials as a safer way to work with containers:

> Environment variables take precedence over configuration
| Name | Description | Default |
|:--------------------------|:---------------------------------------------------------------------|--------:|
| `TAPO_SMARTPLUG_USERNAME` | Define the username for authentication on Tapo SmartPlug integration | `empty` |
| `TAPO_SMARTPLUG_PASSWORD` | Define the password for authentication on Tapo SmartPlug integration | `empty` |
| `WEBHOOK_USERNAME` | Define the username for basic auth on webhooks integration | `empty` |
| `WEBHOOK_PASSWORD` | Define the password for basic auth on webhooks integration | `empty` |

## Examples

Here you have a complete example. More up-to-date one will always be maintained in
Expand Down
8 changes: 4 additions & 4 deletions api/v1alpha1/integrations_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ package v1alpha1
type TapoSmartPlugSpec struct {
Address string `yaml:"address"`
Auth struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
Username string `yaml:"username" env:"TAPO_SMARTPLUG_USERNAME"`
Password string `yaml:"password" env:"TAPO_SMARTPLUG_PASSWORD"`
} `yaml:"auth"`
}

// --
type WebhookSpec struct {
URL string `yaml:"url"`
Auth struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
Username string `yaml:"username" env:"WEBHOOK_USERNAME"`
Password string `yaml:"password" env:"WEBHOOK_PASSWORD"`
} `yaml:"auth"`
}
6 changes: 6 additions & 0 deletions config/samples/autoheater.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ spec:
# Data for sending the events to TAPO P1XX devices (p100, p110, etc)
tapoSmartPlug:
address: "192.168.1.100"

# (Optional) username and password for auth. It's the same used to access the app (it's accessed locally, but
# Tapo devices are configured that way when they are set up)
# Can be defined by env: TAPO_SMARTPLUG_USERNAME, TAPO_SMARTPLUG_PASSWORD
auth:
username: placeholder@gmail.com
password: 'xxxPLACEHOLDERxxx'
Expand All @@ -68,7 +72,9 @@ spec:
# POST <url>: { event: 'start', name: 'pepito', timestamp: ''}
webhook:
url: "https://webhook.site/a7303a4b-4377-49d7-b109-6106fbe21052"

# (Optional) username and password for basic auth
# Can be defined by env: WEBHOOK_USERNAME, WEBHOOK_PASSWORD
auth:
username: 'placeholder'
password: 'placeholder'
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21

require (
github.com/avast/retry-go v3.0.0+incompatible
github.com/caarlos0/env/v9 v9.0.0
github.com/richardjennings/tapo v0.0.1
github.com/spf13/cobra v1.7.0
go.uber.org/zap v1.25.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHS
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/caarlos0/env/v9 v9.0.0 h1:SI6JNsOA+y5gj9njpgybykATIylrRMklbs5ch6wO6pc=
github.com/caarlos0/env/v9 v9.0.0/go.mod h1:ye5mlCVMYh6tZ+vCgrs/B95sj88cg5Tlnc0XIzgZ020=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
16 changes: 13 additions & 3 deletions internal/cmd/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package run

import (
"fmt"
"github.com/caarlos0/env/v9"
"log"
_ "net/http/pprof"
"time"

"github.com/achetronic/autoheater/api/v1alpha1"
"github.com/achetronic/autoheater/internal/config"
"github.com/achetronic/autoheater/internal/schedules"

"github.com/spf13/cobra"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"log"
_ "net/http/pprof"
"time"
)

const (
Expand All @@ -22,6 +25,7 @@ const (
ConfigFlagErrorMessage = "impossible to get flag --config: %s"
LogLevelFlagErrorMessage = "impossible to get flag --log-level: %s"
ConfigNotParsedErrorMessage = "impossible to parse config file: %s"
EnvNotParsedErrorMessage = "impossible to parse environment variables: %s"
)

func NewCommand() *cobra.Command {
Expand Down Expand Up @@ -85,6 +89,12 @@ func RunCommand(cmd *cobra.Command, args []string) {
ctx.Logger.Fatalf(fmt.Sprintf(ConfigNotParsedErrorMessage, err))
}

// Get and parse environment variables for credentials
err = env.Parse(&configContent)
if err != nil {
ctx.Logger.Fatalf(fmt.Sprintf(EnvNotParsedErrorMessage, err))
}

// Set the configuration inside the global context
ctx.Config = &configContent

Expand Down

0 comments on commit 646277c

Please sign in to comment.