Skip to content

Commit

Permalink
Merge pull request #7 from narqo/redact-creds
Browse files Browse the repository at this point in the history
Don't log credentials if one of tokens is missed
  • Loading branch information
narqo authored May 21, 2020
2 parents c7c4aca + 7b6d5da commit ea9b100
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 36 deletions.
6 changes: 2 additions & 4 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
expirationTime: 30
expiration_time: 30

github:
clientID: "github/weather"
endpoint: "https://api.github.com/graphql"
client_id: "github/weather"
token: "$GITHUB_TOKEN"

owm:
api_key: "$OPENWEATHER_API_KEY"
endpoint: "https://api.openweathermap.org/data/2.5/weather?appid={api-key}&units=metric"
query: "Berlin,De"
4 changes: 2 additions & 2 deletions deployments/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ By default, this cronjob gathers `Berlin,De` weather information. If you want to
it in the [`secret.yaml`](secret.yaml) file.

```yaml
expirationTime: 30
expiration_time: 30
github:
...
...
Expand All @@ -35,7 +35,7 @@ It is executed every ten minutes. Wait for it or...

## Test it

Just run
Just run

```bash
$ kubectl create job test --from cronjob/github-weather
Expand Down
4 changes: 2 additions & 2 deletions deployments/secret.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ metadata:
name: gh-config-secret
stringData:
config.yaml: |-
expirationTime: 30
expiration_time: 30
github:
clientID: "github/weather"
client_id: "github/weather"
endpoint: "https://api.github.com/graphql"
token: "$GITHUB_TOKEN"
Expand Down
88 changes: 61 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,75 @@ import (
"syscall"
"time"

"github.com/machinebox/graphql"
"gopkg.in/yaml.v2"
)

"github.com/machinebox/graphql"
const (
defaultOWMAPIEndpoint = "https://api.openweathermap.org/data/2.5/weather?appid={api-key}&units=metric"
defaultGitHubAPIEndpoint = "https://api.github.com/graphql"
defaultGitHubClientID = "github/weather"
)

type Config struct {
ExpirationTime uint8 `yaml:"expirationTime"`
Github struct {
ClientID string `yaml:"clientID"`
ExpirationTime uint8 `yaml:"expiration_time"`
GitHub struct {
ClientID string `yaml:"client_id"`
Endpoint string `yaml:"endpoint"`
Token string `yaml:"token"`
} `yaml:"github"`
Owm struct {
OWM struct {
ApiKey string `yaml:"api_key"`
Endpoint string `yaml:"endpoint"`
Query string `yaml:"query"`
} `yaml:"owm"`
}

func ConfigFromFile(configPath string) (Config, error) {
f, err := os.Open(configPath)
if err != nil {
return Config{}, fmt.Errorf("error opening configuration file %q: %v", configPath, err)
}
defer f.Close()

var cfg Config
if err := yaml.NewDecoder(f).Decode(&cfg); err != nil {
return cfg, fmt.Errorf("error parsing configuration file %q: %v", configPath, err)
}

if cfg.OWM.Endpoint == "" {
cfg.OWM.Endpoint = defaultOWMAPIEndpoint
}

if cfg.GitHub.Endpoint == "" {
cfg.GitHub.Endpoint = defaultGitHubAPIEndpoint
}

if cfg.GitHub.ClientID == "" {
cfg.GitHub.ClientID = defaultGitHubClientID
}

if cfg.ExpirationTime == 0 {
cfg.ExpirationTime = 30
}

if err := validateConfig(cfg); err != nil {
return cfg, fmt.Errorf("error validating configuration file: %v", err)
}

return cfg, nil
}

func validateConfig(cfg Config) error {
if cfg.OWM.ApiKey == "" {
return fmt.Errorf("owm api key is empty")
}
if cfg.GitHub.Token == "" {
return fmt.Errorf("github api token is empty")
}
return nil
}

func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -64,44 +114,28 @@ func run(ctx context.Context, args []string) error {
return err
}

f, err := os.Open(configPath)
cfg, err := ConfigFromFile(configPath)
if err != nil {
return fmt.Errorf("error opening configuration file: %v", err)
}
defer f.Close()

var cfg Config
dec := yaml.NewDecoder(f)
err = dec.Decode(&cfg)
if err != nil {
return fmt.Errorf("error parsing configuration file: %v", err)
}

if cfg.Owm.ApiKey == "" || cfg.Github.Token == "" {
return fmt.Errorf("no API credentials passed: OpenWeather %q, GitHub %q", cfg.Owm.ApiKey, cfg.Github.Token)
}

if cfg.ExpirationTime == 0 {
cfg.ExpirationTime = 30
return err
}

owm := NewOWMClient(cfg.Owm.Endpoint, cfg.Owm.ApiKey)
gh := NewGitHubClient(cfg.Github.Endpoint, cfg.Github.Token)
owm := NewOWMClient(cfg.OWM.Endpoint, cfg.OWM.ApiKey)
gh := NewGitHubClient(cfg.GitHub.Endpoint, cfg.GitHub.Token)
if debug {
gh.client.Log = func(s string) {
log.Println(s)
}
}

wr, err := owm.Weather(ctx, cfg.Owm.Query)
wr, err := owm.Weather(ctx, cfg.OWM.Query)
if err != nil {
return err
}

log.Printf("got owm response: %+v\n", wr)

status := ChangeUserStatusInput{
ClientMutationID: cfg.Github.ClientID,
ClientMutationID: cfg.GitHub.ClientID,
Emoji: wr.Emoji(),
Message: wr.ShortString(),
ExpiresAt: time.Now().UTC().Add(time.Duration(cfg.ExpirationTime) * time.Minute),
Expand Down
4 changes: 3 additions & 1 deletion misc/github-weather.crontab
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*/10 * * * * /home/pi/github-weather.linux-arm7
WORKDIR=/home/pi/.local/lib/github-weather

*/10 * * * * $WORKDIR/github-weather.linux-arm7 -configuration $WORKDIR/config.yaml 2>> /tmp/github-weather.log

0 comments on commit ea9b100

Please sign in to comment.