Skip to content

Commit

Permalink
Add revive linters (#6)
Browse files Browse the repository at this point in the history
* add rvive linters
* linter fixes
* add revive linter bage to readme

Co-authored-by: Budylnikov Vladimir <[email protected]>
  • Loading branch information
nett00n and Budylnikov Vladimir authored Dec 4, 2022
1 parent 95b3a98 commit 4f5d20a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 73 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/linter_revive.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
name: revive linters

on:
push:

permissions:
contents: write
# packages: write
# issues: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-go@v3
with:
go-version: 1.19
- run: go install github.com/mgechev/[email protected]
- run: revive
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Traefik certs exporter

[![revive linters](https://github.com/nett00n/traefik-certs-exporter/actions/workflows/linter_revive.yml/badge.svg?branch=main)](https://github.com/nett00n/traefik-certs-exporter/actions/workflows/linter_revive.yml)

Script opens `acme.json` file from `input` folder and export it's content to `output` folder as `*.cer` and `*.key` files.

There is template `acme.json` file in `input` directory for demonstration purposes. It is added in `.gitignore` - feel free to modify or delete it.
Expand Down
3 changes: 3 additions & 0 deletions golangci-lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters:
enable:
- revive
149 changes: 76 additions & 73 deletions traefik-certs-exporter.go
Original file line number Diff line number Diff line change
@@ -1,92 +1,95 @@
// exports traefik's certificates as files
package main

import (
"encoding/json"
"encoding/base64"
"io/ioutil"
"os"
"log"
"encoding/base64"
"encoding/json"
"io/ioutil"
"log"
"os"
)

// ACME is an imported global traefik acme.json sctructure
type ACME struct {
Le struct {
Account struct {
Email string
} `json: "Account"`
Certificates []Certificates
} `json: "le"`
Le struct {
Account struct {
Email string
} `json: "Account"`
Certificates []Certificates
} `json: "le"`
}

// Certificates list in acme.json
type Certificates struct {
Domain struct {
Main string `json: "main"`
}`json: "domain"`
Certificate string `json: "certificate"`
Key string `json: "key"`
Domain struct {
Main string `json: "main"`
} `json: "domain"`
Certificate string `json: "certificate"`
Key string `json: "key"`
}

func main() {
// open json file
jsonFile, err := os.Open("input/acme.json")
// if os.Open returns an error then print out it
if err != nil {
log.Println(err)
}
log.Println("Successfully Opened acme.json")
// defer the closing of the json file
defer jsonFile.Close()
// open json file
jsonFile, err := os.Open("input/acme.json")
// if os.Open returns an error then print out it
if err != nil {
log.Println(err)
}
log.Println("Successfully Opened acme.json")
// defer the closing of the json file
defer jsonFile.Close()

// create directory for certs
path := "output"
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
log.Println(err)
}
// create directory for certs
path := "output"
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
log.Println(err)
}

byteValue, _ := ioutil.ReadAll(jsonFile)
var traefik ACME
err = json.Unmarshal(byteValue, &traefik)
if err != nil {
panic(err)
}
for i := 0; i < len(traefik.Le.Certificates); i++ {
// print cert hostname
log.Println("Certificate host:", traefik.Le.Certificates[i].Domain.Main)
// decode and print certificate
cert_body, err := base64.StdEncoding.DecodeString(traefik.Le.Certificates[i].Certificate)
if err != nil {
log.Println(err)
}
log.Println("Save certificate body", traefik.Le.Certificates[i].Domain.Main)
// create cert file
cert_file, err := os.Create("output/"+ traefik.Le.Certificates[i].Domain.Main + ".cer")
if err != nil {
log.Println(err)
}
defer cert_file.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var traefik ACME
err = json.Unmarshal(byteValue, &traefik)
if err != nil {
panic(err)
}
for i := 0; i < len(traefik.Le.Certificates); i++ {
// print cert hostname
log.Println("Certificate host:", traefik.Le.Certificates[i].Domain.Main)
// decode and print certificate
certBody, err := base64.StdEncoding.DecodeString(traefik.Le.Certificates[i].Certificate)
if err != nil {
log.Println(err)
}
log.Println("Save certificate body", traefik.Le.Certificates[i].Domain.Main)
// create cert file
certFile, err := os.Create("output/" + traefik.Le.Certificates[i].Domain.Main + ".cer")
if err != nil {
log.Println(err)
}
defer certFile.Close()

_, err = cert_file.Write(cert_body)
if err != nil {
log.Println(err)
}
_, err = certFile.Write(certBody)
if err != nil {
log.Println(err)
}

// decode and print key
cert_key, err := base64.StdEncoding.DecodeString(traefik.Le.Certificates[i].Key)
if err != nil {
log.Println(err)
}
log.Println("Save certificate key", traefik.Le.Certificates[i].Domain.Main)
// create cert file
key_file, err := os.Create("output/"+ traefik.Le.Certificates[i].Domain.Main + ".key")
if err != nil {
log.Println(err)
}
defer key_file.Close()
// decode and print key
certKey, err := base64.StdEncoding.DecodeString(traefik.Le.Certificates[i].Key)
if err != nil {
log.Println(err)
}
log.Println("Save certificate key", traefik.Le.Certificates[i].Domain.Main)
// create cert file
keyFile, err := os.Create("output/" + traefik.Le.Certificates[i].Domain.Main + ".key")
if err != nil {
log.Println(err)
}
defer keyFile.Close()

_, err = key_file.Write(cert_key)
if err != nil {
log.Println(err)
}
_, err = keyFile.Write(certKey)
if err != nil {
log.Println(err)
}

}
}
}

0 comments on commit 4f5d20a

Please sign in to comment.