From a08df819677d754e4f818cc8a931d19d0f76f9ed Mon Sep 17 00:00:00 2001 From: Adora Laura Kalb Date: Fri, 28 Jun 2024 15:28:54 +0200 Subject: [PATCH] initial commit --- .gitignore | 29 +++++++++++++++++++++++++++++ LICENSE | 1 + cmd/certwarden-deploy/root.go | 22 ++++++++++++++++++++++ config.yaml | 8 ++++++++ go.mod | 15 +++++++++++++++ go.sum | 21 +++++++++++++++++++++ internal/cli/root.go | 17 +++++++++++++++++ internal/config/config.go | 26 ++++++++++++++++++++++++++ internal/config/models.go | 20 ++++++++++++++++++++ internal/constants/constants.go | 7 +++++++ internal/init/init.go | 18 ++++++++++++++++++ internal/logger/logger.go | 18 ++++++++++++++++++ main.go | 11 +++++++++++ 13 files changed, 213 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 cmd/certwarden-deploy/root.go create mode 100644 config.yaml create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/cli/root.go create mode 100644 internal/config/config.go create mode 100644 internal/config/models.go create mode 100644 internal/constants/constants.go create mode 100644 internal/init/init.go create mode 100644 internal/logger/logger.go create mode 100644 main.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b51c53 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +# ---> Go +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work + +bin/ + +examples/testing/ + +*.toml +!examples/*.toml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..0d4ef8b --- /dev/null +++ b/LICENSE @@ -0,0 +1 @@ +Copyright © 2024 Laura Kalb \ No newline at end of file diff --git a/cmd/certwarden-deploy/root.go b/cmd/certwarden-deploy/root.go new file mode 100644 index 0000000..febbb53 --- /dev/null +++ b/cmd/certwarden-deploy/root.go @@ -0,0 +1,22 @@ +/* +Copyright © 2024 Laura Kalb +The code of this project is available under the MIT license. See the LICENSE file for more info. +*/ +package cmd + +import ( + "os" + + "code.lila.network/adoralaura/certwarden-deploy/internal/cli" +) + +var cfgFile string + +// Execute adds all child commands to the root command and sets flags appropriately. +// This is called by main.main(). It only needs to happen once to the rootCmd. +func Execute() { + err := cli.RootCmd.Execute() + if err != nil { + os.Exit(1) + } +} diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..194bb99 --- /dev/null +++ b/config.yaml @@ -0,0 +1,8 @@ +base_path: "https://certs.lauka-home.net" +disable_certificate_validation: true + + +certificates: + - name: test.laura.ovh + api_key: CiwnqR3rF1KZhGCRMsydSHkKksdeeFPA + action: "" diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3be14ba --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module code.lila.network/adoralaura/certwarden-deploy + +go 1.22.2 + +require ( + github.com/spf13/cobra v1.8.1 + gopkg.in/yaml.v3 v3.0.1 +) + +require ( + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/kr/pretty v0.3.1 // indirect + github.com/spf13/pflag v1.0.5 // indirect + gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..66ad4e7 --- /dev/null +++ b/go.sum @@ -0,0 +1,21 @@ +github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= +github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/cli/root.go b/internal/cli/root.go new file mode 100644 index 0000000..b21433c --- /dev/null +++ b/internal/cli/root.go @@ -0,0 +1,17 @@ +package cli + +import "github.com/spf13/cobra" + +var RootCmd = &cobra.Command{ + Use: "certwarden-deploy", + Short: "A brief description of your application", + Long: `A longer description that spans multiple lines and likely contains +examples and usage of using your application. For example: + +Cobra is a CLI library for Go that empowers applications. +This application is a tool to generate the needed files +to quickly create a Cobra application.`, + // Uncomment the following line if your bare application + // has an action associated with it: + // Run: func(cmd *cobra.Command, args []string) { }, +} diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000..3416b1c --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,26 @@ +package config + +import ( + "log/slog" + "os" + + "gopkg.in/yaml.v3" +) + +func InitializeConfig() { + if *ConfigFile != "" { + *ConfigFile = "/etc/certwarden-deploy/config.yaml" + } + + data, err := os.ReadFile(*ConfigFile) + if err != nil { + slog.Error("failed to read config file", "file", *ConfigFile, "error", err) + os.Exit(1) + } + + err = yaml.Unmarshal([]byte(data), &Config) + if err != nil { + slog.Error("failed to unmarshal config file", "file", *ConfigFile, "error", err) + os.Exit(1) + } +} diff --git a/internal/config/models.go b/internal/config/models.go new file mode 100644 index 0000000..42d5e29 --- /dev/null +++ b/internal/config/models.go @@ -0,0 +1,20 @@ +package config + +import "log/slog" + +var Config *ConfigFileData +var ConfigFile *string +var Logger *slog.Logger + +type ConfigFileData struct { + BaseURL string `yaml:"base_url"` + DisableCertificateValidation bool `yaml:"disable_certificate_validation"` + Certificates []CertificateData `yaml:"certificates"` +} + +type CertificateData struct { + Name string `yaml:"name"` + ApiKey string `yaml:"api_key"` + Action string `yaml:"action"` + FilePath string `yaml:"file_path"` +} diff --git a/internal/constants/constants.go b/internal/constants/constants.go new file mode 100644 index 0000000..e69995a --- /dev/null +++ b/internal/constants/constants.go @@ -0,0 +1,7 @@ +package constants + +const Version = "0.0.1" +const ChainApiPath = "/certwarden/api/v1/download/certrootchains/" +const CertificateApiPath = "/certwarden/api/v1/download/certificates/" +const ApiKeyHeaderName = "X-API-Key" +const UserAgent = "certwarden-deploy/" + Version + " +code.lila.network/adoralaura/certwarden-deploy" diff --git a/internal/init/init.go b/internal/init/init.go new file mode 100644 index 0000000..b2c4580 --- /dev/null +++ b/internal/init/init.go @@ -0,0 +1,18 @@ +package init + +import ( + "code.lila.network/adoralaura/certwarden-deploy/internal/cli" + "code.lila.network/adoralaura/certwarden-deploy/internal/config" + "code.lila.network/adoralaura/certwarden-deploy/internal/logger" + "github.com/spf13/cobra" +) + +func InitializeApp() { + cobra.OnInitialize(config.InitializeConfig, logger.InitializeLogger) + + // Here you will define your flags and configuration settings. + // Cobra supports persistent flags, which, if defined here, + // will be global for your application. + + cli.RootCmd.PersistentFlags().StringVar(config.ConfigFile, "config", "", "config file (default is /etc/certwarden-deploy/config.yaml)") +} diff --git a/internal/logger/logger.go b/internal/logger/logger.go new file mode 100644 index 0000000..4cb074c --- /dev/null +++ b/internal/logger/logger.go @@ -0,0 +1,18 @@ +package logger + +import ( + "log/slog" + "os" +) + +func InitializeLogger() { + // TODO: Different Log levels + + opts := &slog.HandlerOptions{ + Level: slog.LevelInfo, + } + + handler := slog.NewTextHandler(os.Stdout, opts) + + slog.SetDefault(slog.New(handler)) +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..588fd19 --- /dev/null +++ b/main.go @@ -0,0 +1,11 @@ +/* +Copyright © 2024 Laura Kalb +The code of this project is available under the MIT license. See the LICENSE file for more info. +*/ +package main + +import cmd "code.lila.network/adoralaura/certwarden-deploy/cmd/certwarden-deploy" + +func main() { + cmd.Execute() +}