github action + minor refactor

This commit is contained in:
mkelcik
2023-04-29 12:45:21 +02:00
parent 2a23c41aaa
commit 3921b648ce
3 changed files with 78 additions and 28 deletions

31
.github/workflows/image-scanner.yml vendored Normal file
View File

@@ -0,0 +1,31 @@
name: build
on:
workflow_run:
workflows: [ "Code check" ]
types:
- completed
jobs:
on-failure:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
- run: echo 'The triggering workflow failed'
build:
name: Image vulnerability scanner
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-20.04
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Build an image from Dockerfile
run: |
docker build -t docker.io/my-organization/my-app:${{ github.sha }} .
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: 'docker.io/my-organization/my-app:${{ github.sha }}'
format: 'table'
exit-code: '1'
ignore-unfixed: true
vuln-type: 'os,library'
severity: 'CRITICAL,HIGH'

64
main.go
View File

@@ -4,7 +4,6 @@ import (
"context"
"log"
"net"
"net/http"
"os/signal"
"syscall"
"time"
@@ -24,9 +23,7 @@ func getResolver(resolverName string) PublicIpResolver {
case public_resolvers.IfConfigMeTag:
fallthrough
default:
return public_resolvers.NewIfConfigMe(&http.Client{
Timeout: 10 * time.Second,
})
return public_resolvers.NewDefaultIfConfigMe()
}
}
@@ -57,33 +54,46 @@ func main() {
log.Fatal(err)
}
dns, err := allDNSRecords(ctx, api, cloudflare.ZoneIdentifier(zoneID))
if err != nil {
log.Fatal(err)
}
log.Println("waiting for update tick ...")
ticker := time.NewTicker(config.CheckInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
log.Println("tick received checking ...")
func() {
dns, err := allDNSRecords(ctx, api, cloudflare.ZoneIdentifier(zoneID))
if err != nil {
log.Fatal(err)
}
for _, dnsRecord := range dns {
if internal.Contains(config.DnsRecordsToCheck, dnsRecord.Name) {
log.Printf("Checking record `%s` with current value `%s` ...", dnsRecord.Name, dnsRecord.Content)
if currentPublicIP.String() == dnsRecord.Content {
log.Println("OK")
continue // no update needed
}
for _, dnsRecord := range dns {
if internal.Contains(config.DnsRecordsToCheck, dnsRecord.Name) {
log.Printf("Checking record `%s` with current value `%s` ...", dnsRecord.Name, dnsRecord.Content)
if currentPublicIP.String() == dnsRecord.Content {
log.Println("OK")
continue // no update needed
}
update := cloudflare.UpdateDNSRecordParams{
ID: dnsRecord.ID,
Content: currentPublicIP.String(),
}
update := cloudflare.UpdateDNSRecordParams{
ID: dnsRecord.ID,
Content: currentPublicIP.String(),
}
if config.OnChangeComment != "" {
update.Comment = config.OnChangeComment
}
if config.OnChangeComment != "" {
update.Comment = config.OnChangeComment
}
if _, err := api.UpdateDNSRecord(ctx, cloudflare.ZoneIdentifier(zoneID), update); err != nil {
log.Printf("error updating dns record: %s", err)
} else {
log.Printf("Updated to `%s`", currentPublicIP)
}
if _, err := api.UpdateDNSRecord(ctx, cloudflare.ZoneIdentifier(zoneID), update); err != nil {
log.Printf("error updating dns record: %s", err)
} else {
log.Printf("Updated to `%s`", currentPublicIP)
}
}
}
}()
case <-ctx.Done():
break
}
}
}

View File

@@ -6,6 +6,7 @@ import (
"io"
"net"
"net/http"
"time"
)
const (
@@ -24,6 +25,12 @@ type IfConfigMe struct {
client Doer
}
func NewDefaultIfConfigMe() *IfConfigMe {
return NewIfConfigMe(&http.Client{
Timeout: 10 * time.Second,
})
}
func NewIfConfigMe(c Doer) *IfConfigMe {
return &IfConfigMe{client: c}
}
@@ -38,7 +45,9 @@ func (i IfConfigMe) ResolvePublicIp(ctx context.Context) (net.IP, error) {
if err != nil {
return net.IP{}, err
}
defer resp.Body.Close()
defer func() {
_ = resp.Body.Close()
}()
if resp.StatusCode != http.StatusOK {
return net.IP{}, fmt.Errorf("unexpected response code %d", resp.StatusCode)