-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add certificate provider * lint: suppress warn and simplify * rename service file name
- Loading branch information
Showing
6 changed files
with
148 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package external | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// CertificateProvider is a status provider that check SSL certificate | ||
type CertificateProvider struct { | ||
TimeOut time.Duration | ||
} | ||
|
||
// Status url looks like: cert://example.com. It will try to get SSL certificate and check if it is valid and not going to expire soon | ||
func (c *CertificateProvider) Status(req Request) (*Response, error) { | ||
st := time.Now() | ||
addr := strings.TrimPrefix(req.URL, "cert://") + ":443" | ||
conn, err := tls.Dial("tcp", addr, &tls.Config{}) //nolint:gosec // we don't care about cert version | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "failed to connect to %s", addr) | ||
} | ||
if err = conn.Handshake(); err != nil { | ||
return nil, errors.Wrapf(err, "failed to handshake with %s", addr) | ||
} | ||
defer conn.Close() // nolint | ||
|
||
certs := conn.ConnectionState().PeerCertificates | ||
earlierCert := time.Date(2150, 1, 1, 0, 0, 0, 0, time.UTC) | ||
for _, cert := range certs { | ||
if cert.NotAfter.Before(earlierCert) { | ||
earlierCert = cert.NotAfter | ||
} | ||
} | ||
|
||
daysLeft := int(time.Until(earlierCert).Hours() / 24) | ||
body := map[string]interface{}{ | ||
"expire": earlierCert.Format(time.RFC3339), | ||
"days_left": daysLeft, | ||
"host": strings.Replace(req.URL, "cert://", "https://", 1), | ||
"status": "ok", | ||
} | ||
if daysLeft < 5 { | ||
body["status"] = fmt.Sprintf("expiring soon, in %d days", daysLeft) | ||
} | ||
if earlierCert.Before(time.Now()) { | ||
body["status"] = "expired" | ||
} | ||
|
||
result := Response{ | ||
Name: req.Name, | ||
StatusCode: 200, | ||
Body: body, | ||
ResponseTime: time.Since(st).Milliseconds(), | ||
} | ||
return &result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package external | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCertificateProvider_Status(t *testing.T) { | ||
cp := CertificateProvider{TimeOut: time.Minute} | ||
resp, err := cp.Status(Request{Name: "test", URL: "cert://umputun.com"}) | ||
require.NoError(t, err) | ||
t.Logf("%+v", resp) | ||
assert.Equal(t, "test", resp.Name) | ||
assert.Equal(t, 200, resp.StatusCode) | ||
assert.Equal(t, "ok", resp.Body["status"]) | ||
assert.Equal(t, "https://umputun.com", resp.Body["host"]) | ||
|
||
exp, err := time.Parse(time.RFC3339, resp.Body[`expire`].(string)) | ||
require.NoError(t, err) | ||
assert.True(t, exp.After(time.Now().Add(5*24*time.Hour))) | ||
t.Logf("expire: %+v", exp) | ||
} | ||
|
||
func TestCertificateProvider_StatusFailed(t *testing.T) { | ||
cp := CertificateProvider{TimeOut: time.Minute} | ||
_, err := cp.Status(Request{Name: "test", URL: "cert://127.0.0.1"}) | ||
require.Error(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters