-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from shogo82148/update-cetificates-2023-08-30
update the certificates (2023-08-30)
- Loading branch information
Showing
8 changed files
with
359 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: update | ||
on: | ||
schedule: | ||
- cron: "23 6 * * *" | ||
workflow_dispatch: | ||
|
||
jobs: | ||
update: | ||
name: update | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Check out code into the Go module directory | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: stable | ||
|
||
- name: Update the Certificates | ||
run: go generate ./... | ||
|
||
- name: Commit and Push Changes | ||
uses: shogo82148/actions-commit-and-create-pr@v1 |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/shogo82148/rdsmysql | ||
|
||
go 1.13 | ||
go 1.16 | ||
|
||
require ( | ||
github.com/aws/aws-sdk-go v1.44.313 | ||
|
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
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,121 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"fmt" | ||
"go/format" | ||
"io" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
func main() { | ||
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) | ||
defer cancel() | ||
|
||
err := downloadCertificate(ctx, &options{ | ||
file: "rds.go", | ||
pkg: "certificate", | ||
url: "https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem", | ||
name: "rdsCertificates", | ||
comment: `// rdsCertificates is the intermediate and root [certificates] for [Amazon RDS MySQL] and [Amazon Aurora MySQL]. | ||
// | ||
// [Amazon RDS MySQL]: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.SSL.html#UsingWithRDS.SSL.CertificatesAllRegions | ||
// [Amazon Aurora MySQL]: https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/UsingWithRDS.SSL.html#UsingWithRDS.SSL.CertificatesAllRegions | ||
// [certificates]: https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem | ||
`, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
type options struct { | ||
file string | ||
pkg string | ||
url string | ||
name string | ||
comment string | ||
} | ||
|
||
func downloadCertificate(ctx context.Context, opts *options) error { | ||
pemCerts, err := download(ctx, opts.url) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
certs, err := parseCertificate(pemCerts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
buf := &bytes.Buffer{} | ||
buf.WriteString("// Code generated by cmd/update_certificate/main.go; DO NOT EDIT.\n\n") | ||
buf.WriteString("package " + opts.pkg + "\n\n") | ||
buf.WriteString(opts.comment) | ||
buf.WriteString("const " + opts.name + " = `") | ||
buf.Write(pemCerts) | ||
buf.WriteString("`\n\n") | ||
|
||
buf.WriteString("// " + opts.name + " contains:\n") | ||
buf.WriteString("//\n") | ||
for _, cert := range certs { | ||
nbf := cert.NotBefore.Format(time.RFC3339) | ||
naf := cert.NotAfter.Format(time.RFC3339) | ||
fmt.Fprintf(buf, "// - %50s (not before: %s, not after: %s)\n", cert.Subject.CommonName, nbf, naf) | ||
} | ||
|
||
data, err := format.Source(buf.Bytes()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return os.WriteFile(opts.file, data, 0644) | ||
} | ||
|
||
func download(ctx context.Context, url string) ([]byte, error) { | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
} | ||
|
||
return io.ReadAll(resp.Body) | ||
} | ||
|
||
func parseCertificate(pemCerts []byte) ([]*x509.Certificate, error) { | ||
var certs []*x509.Certificate | ||
for len(pemCerts) > 0 { | ||
var block *pem.Block | ||
block, pemCerts = pem.Decode(pemCerts) | ||
if block == nil { | ||
break | ||
} | ||
if block.Type != "CERTIFICATE" || len(block.Headers) != 0 { | ||
continue | ||
} | ||
|
||
certBytes := block.Bytes | ||
cert, err := x509.ParseCertificate(certBytes) | ||
if err != nil { | ||
return nil, err | ||
} | ||
certs = append(certs, cert) | ||
} | ||
return certs, nil | ||
} |