Skip to content

Commit ea9b100

Browse files
authoredMay 21, 2020
Merge pull request #7 from narqo/redact-creds
Don't log credentials if one of tokens is missed
2 parents c7c4aca + 7b6d5da commit ea9b100

File tree

5 files changed

+70
-36
lines changed

5 files changed

+70
-36
lines changed
 

‎config.yaml

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
expirationTime: 30
1+
expiration_time: 30
22

33
github:
4-
clientID: "github/weather"
5-
endpoint: "https://api.github.com/graphql"
4+
client_id: "github/weather"
65
token: "$GITHUB_TOKEN"
76

87
owm:
98
api_key: "$OPENWEATHER_API_KEY"
10-
endpoint: "https://api.openweathermap.org/data/2.5/weather?appid={api-key}&units=metric"
119
query: "Berlin,De"

‎deployments/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ By default, this cronjob gathers `Berlin,De` weather information. If you want to
1515
it in the [`secret.yaml`](secret.yaml) file.
1616

1717
```yaml
18-
expirationTime: 30
18+
expiration_time: 30
1919
github:
2020
...
2121
...
@@ -35,7 +35,7 @@ It is executed every ten minutes. Wait for it or...
3535

3636
## Test it
3737

38-
Just run
38+
Just run
3939

4040
```bash
4141
$ kubectl create job test --from cronjob/github-weather

‎deployments/secret.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ metadata:
44
name: gh-config-secret
55
stringData:
66
config.yaml: |-
7-
expirationTime: 30
7+
expiration_time: 30
88
99
github:
10-
clientID: "github/weather"
10+
client_id: "github/weather"
1111
endpoint: "https://api.github.com/graphql"
1212
token: "$GITHUB_TOKEN"
1313

‎main.go

+61-27
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,75 @@ import (
1414
"syscall"
1515
"time"
1616

17+
"github.com/machinebox/graphql"
1718
"gopkg.in/yaml.v2"
19+
)
1820

19-
"github.com/machinebox/graphql"
21+
const (
22+
defaultOWMAPIEndpoint = "https://api.openweathermap.org/data/2.5/weather?appid={api-key}&units=metric"
23+
defaultGitHubAPIEndpoint = "https://api.github.com/graphql"
24+
defaultGitHubClientID = "github/weather"
2025
)
2126

2227
type Config struct {
23-
ExpirationTime uint8 `yaml:"expirationTime"`
24-
Github struct {
25-
ClientID string `yaml:"clientID"`
28+
ExpirationTime uint8 `yaml:"expiration_time"`
29+
GitHub struct {
30+
ClientID string `yaml:"client_id"`
2631
Endpoint string `yaml:"endpoint"`
2732
Token string `yaml:"token"`
2833
} `yaml:"github"`
29-
Owm struct {
34+
OWM struct {
3035
ApiKey string `yaml:"api_key"`
3136
Endpoint string `yaml:"endpoint"`
3237
Query string `yaml:"query"`
3338
} `yaml:"owm"`
3439
}
3540

41+
func ConfigFromFile(configPath string) (Config, error) {
42+
f, err := os.Open(configPath)
43+
if err != nil {
44+
return Config{}, fmt.Errorf("error opening configuration file %q: %v", configPath, err)
45+
}
46+
defer f.Close()
47+
48+
var cfg Config
49+
if err := yaml.NewDecoder(f).Decode(&cfg); err != nil {
50+
return cfg, fmt.Errorf("error parsing configuration file %q: %v", configPath, err)
51+
}
52+
53+
if cfg.OWM.Endpoint == "" {
54+
cfg.OWM.Endpoint = defaultOWMAPIEndpoint
55+
}
56+
57+
if cfg.GitHub.Endpoint == "" {
58+
cfg.GitHub.Endpoint = defaultGitHubAPIEndpoint
59+
}
60+
61+
if cfg.GitHub.ClientID == "" {
62+
cfg.GitHub.ClientID = defaultGitHubClientID
63+
}
64+
65+
if cfg.ExpirationTime == 0 {
66+
cfg.ExpirationTime = 30
67+
}
68+
69+
if err := validateConfig(cfg); err != nil {
70+
return cfg, fmt.Errorf("error validating configuration file: %v", err)
71+
}
72+
73+
return cfg, nil
74+
}
75+
76+
func validateConfig(cfg Config) error {
77+
if cfg.OWM.ApiKey == "" {
78+
return fmt.Errorf("owm api key is empty")
79+
}
80+
if cfg.GitHub.Token == "" {
81+
return fmt.Errorf("github api token is empty")
82+
}
83+
return nil
84+
}
85+
3686
func main() {
3787
ctx, cancel := context.WithCancel(context.Background())
3888
defer cancel()
@@ -64,44 +114,28 @@ func run(ctx context.Context, args []string) error {
64114
return err
65115
}
66116

67-
f, err := os.Open(configPath)
117+
cfg, err := ConfigFromFile(configPath)
68118
if err != nil {
69-
return fmt.Errorf("error opening configuration file: %v", err)
70-
}
71-
defer f.Close()
72-
73-
var cfg Config
74-
dec := yaml.NewDecoder(f)
75-
err = dec.Decode(&cfg)
76-
if err != nil {
77-
return fmt.Errorf("error parsing configuration file: %v", err)
78-
}
79-
80-
if cfg.Owm.ApiKey == "" || cfg.Github.Token == "" {
81-
return fmt.Errorf("no API credentials passed: OpenWeather %q, GitHub %q", cfg.Owm.ApiKey, cfg.Github.Token)
82-
}
83-
84-
if cfg.ExpirationTime == 0 {
85-
cfg.ExpirationTime = 30
119+
return err
86120
}
87121

88-
owm := NewOWMClient(cfg.Owm.Endpoint, cfg.Owm.ApiKey)
89-
gh := NewGitHubClient(cfg.Github.Endpoint, cfg.Github.Token)
122+
owm := NewOWMClient(cfg.OWM.Endpoint, cfg.OWM.ApiKey)
123+
gh := NewGitHubClient(cfg.GitHub.Endpoint, cfg.GitHub.Token)
90124
if debug {
91125
gh.client.Log = func(s string) {
92126
log.Println(s)
93127
}
94128
}
95129

96-
wr, err := owm.Weather(ctx, cfg.Owm.Query)
130+
wr, err := owm.Weather(ctx, cfg.OWM.Query)
97131
if err != nil {
98132
return err
99133
}
100134

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

103137
status := ChangeUserStatusInput{
104-
ClientMutationID: cfg.Github.ClientID,
138+
ClientMutationID: cfg.GitHub.ClientID,
105139
Emoji: wr.Emoji(),
106140
Message: wr.ShortString(),
107141
ExpiresAt: time.Now().UTC().Add(time.Duration(cfg.ExpirationTime) * time.Minute),

‎misc/github-weather.crontab

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
*/10 * * * * /home/pi/github-weather.linux-arm7
1+
WORKDIR=/home/pi/.local/lib/github-weather
2+
3+
*/10 * * * * $WORKDIR/github-weather.linux-arm7 -configuration $WORKDIR/config.yaml 2>> /tmp/github-weather.log

0 commit comments

Comments
 (0)
Please sign in to comment.