-
Notifications
You must be signed in to change notification settings - Fork 0
/
certcheck.go
186 lines (173 loc) · 4.39 KB
/
certcheck.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"runtime"
"time"
yaml "gopkg.in/yaml.v2"
)
var (
// Version - certcheck version
Version = "{version}"
// Revision - revision version
Revision = "{revision}"
)
// Data - Setting File Struct
type Data struct {
Targets []Target `yaml:"targets"`
Slack Slack `yaml:"slack"`
}
// Target - Cert Check Target Struct
type Target struct {
Name string `yaml:"name"`
Endpoint string `yaml:"endpoint"`
Threshold int `yaml:"threshold"`
URL string `yaml:"hook_url"`
Channel string `yaml:"channel"`
Username string `yaml:"username"`
Icon string `yaml:"icon"`
}
// Slack - Notify Setting Struct
type Slack struct {
URL string `yaml:"hook_url"`
Channel string `yaml:"channel"`
Username string `yaml:"username"`
Icon string `yaml:"icon"`
}
// SlackJSON - Slack Properties
type SlackJSON struct {
Channel string `json:"channel"`
Username string `json:"username"`
Text string `json:"text"`
Icon string `json:"icon_emoji"`
}
func main() {
filename := flag.String("c", "certcheck.yml", "config file name")
isVersion1 := flag.Bool("v", false, "prints current version")
isVersion2 := flag.Bool("version", false, "prints current version")
flag.Parse()
if *isVersion1 || *isVersion2 {
log.Printf(getVersion())
} else {
buf, err := ioutil.ReadFile(*filename)
if err != nil {
log.Fatal(err)
return
}
var d Data
err = yaml.Unmarshal(buf, &d)
if err != nil {
log.Fatal(err)
return
}
for key, tgt := range d.Targets {
if checkParam(key, tgt) {
message, result := getAPI(tgt.Endpoint, tgt.Threshold)
if result {
postSlack(d.Slack, tgt, message)
}
log.Printf(message)
}
}
}
}
func getVersion() string {
return fmt.Sprintf("%s %s/%s, build %s", Version, runtime.GOOS, runtime.GOARCH, Revision)
}
func checkParam(key int, tgt Target) bool {
var result = true
if tgt.Name == "" {
log.Printf("name is empty. (key: %d): tgt.Endpoint", key)
result = false
}
if tgt.Endpoint == "" {
log.Printf("endpoint is empty. (key: %d): tgt.Endpoint", key)
result = false
}
if tgt.Threshold < 0 {
log.Printf("threshold is less than 0. (key: %d): tgt.Endpoint", key)
result = false
}
return result
}
/**
* Portions are:
* Copyright (C) 2017 ynozue (https://github.com/ynozue)
* Originally under Apache License Version 2.0, http://www.apache.org/licenses/LICENSE-2.0
**/
func getAPI(endpoint string, threshold int) (string, bool) {
var message = ""
var result = true
resp, err := http.Get(endpoint)
if err != nil {
message = fmt.Sprintf("NG: %s", err)
} else {
defer resp.Body.Close()
expire := "-"
if len(resp.TLS.PeerCertificates) > 0 {
expireUTCTime := resp.TLS.PeerCertificates[0].NotAfter
expireJSTTime := expireUTCTime.In(time.FixedZone("Asia/Tokyo", 9*60*60))
expire = expireJSTTime.Format("2006/01/02 15:04")
th := time.Now().AddDate(0, 0, threshold)
since := int(expireJSTTime.Sub(time.Now()).Hours() / 24)
if !th.Before(expireJSTTime) {
message = fmt.Sprintf("Cert Warning: %s expire: %s at %d days", endpoint, expire, since)
} else {
message = fmt.Sprintf("Cert OK: %s expire: %s at %d days", endpoint, expire, since)
result = false
}
}
}
return message, result
}
func postSlack(slack Slack, tgt Target, message string) bool {
slackProperties := SlackJSON{}
// Channel
if slack.Channel != "" {
slackProperties.Channel = slack.Channel
}
if tgt.Channel != "" {
slackProperties.Channel = tgt.Channel
}
// Username
if slack.Username != "" {
slackProperties.Username = slack.Username
}
if tgt.Username != "" {
slackProperties.Username = tgt.Username
}
// Text
slackProperties.Text = message
// Icon
if slack.Icon != "" {
slackProperties.Icon = slack.Icon
}
if tgt.Icon != "" {
slackProperties.Icon = tgt.Icon
}
params, _ := json.Marshal(slackProperties)
var slackURL = slack.URL
if tgt.URL != "" {
slackURL = tgt.URL
}
resp, err := http.PostForm(
slackURL,
url.Values{"payload": {string(params)}},
)
if err != nil {
log.Fatal(err)
return false
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
return false
}
return true
}