Skip to content

Commit

Permalink
update projects.
Browse files Browse the repository at this point in the history
  • Loading branch information
hurricane1988 committed Jul 13, 2024
1 parent 218d7f9 commit ce3a64e
Show file tree
Hide file tree
Showing 13 changed files with 868 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
### Go template
# 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

4 changes: 4 additions & 0 deletions .idea/certificate-generater.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 89 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

# Setting SHELL to bash allows bash commands to be executed by recipes.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec

.PHONY: all
all: build

##@ General

# The help target prints out all targets with their descriptions organized
# beneath their categories. The categories are represented by '##@' and the
# target descriptions by '##'. The awk command is responsible for reading the
# entire set of makefiles included in this invocation, looking for lines of the
# file as xyz: ## something, and then pretty-format the target and help. Then,
# if there's a line with ##@ something, that gets pretty-printed as a category.
# More info on the usage of ANSI control characters for terminal formatting:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php

.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Development

.PHONY: fmt
fmt: ## Run go fmt against code.
go fmt ./...

.PHONY: vet
vet: ## Run go vet against code.
go vet ./...

##@ Build

.PHONY: build
build: fmt vet ## Build cert-generator binary.
go build -o bin/cert-generator cmd/main.go

.PHONY: run
run: fmt vet ## Run a cert-generator from your host.
go run ./cmd/main.go


## Location to install dependencies to
LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
# $1 - target path with name of binary (ideally with version)
# $2 - package url which can be installed
# $3 - specific version of package
define go-install-tool
@[ -f $(1) ] || { \
set -e; \
package=$(2)@$(3) ;\
echo "Downloading $${package}" ;\
GOBIN=$(LOCALBIN) go install $${package} ;\
mv "$$(echo "$(1)" | sed "s/-$(3)$$//")" $(1) ;\
}
endef
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
## Cert Generator

[]()

---

### Description

---
cert-generator is a tool to generate certs and to simplify the process and complexity of generating certificates.

### Getting Started

---

- [x] build the exec binary.
```shell
make build
```

```shell
bin/cert-generator
```
- [x] get help information
```shell
bin/cert-generator --help
```

```shell
Usage of bin/cert-generator:
-ca-common-name string
The common name of CA.
-ca-country string
The country of CA, multiple items separated by ',', Default: CN. (default "CN")
-ca-domains string
The domain of CA, multiple items separated by ','.
-ca-organization string
The organization of CA, multiple items separated by ','
-ca-years int
The validate years of CA, Default: 50. (default 50)
-cert-path string
The path to save certificate. (default "/tmp")
-kubeconfig string
Paths to a kubeconfig. Only required if out-of-cluster.
-zap-devel
Development Mode defaults(encoder=consoleEncoder,logLevel=Debug,stackTraceLevel=Warn). Production Mode defaults(encoder=jsonEncoder,logLevel=Info,stackTraceLevel=Error)
-zap-encoder value
Zap log encoding (one of 'json' or 'console')
-zap-log-level value
Zap Level to configure the verbosity of logging. Can be one of 'debug', 'info', 'error', or any integer value > 0 which corresponds to custom debug levels of increasing verbosity
-zap-stacktrace-level value
Zap Level at and above which stacktraces are captured (one of 'info', 'error', 'panic').
-zap-time-encoding value
Zap time encoding (one of 'epoch', 'millis', 'nano', 'iso8601', 'rfc3339' or 'rfc3339nano'). Defaults to 'epoch'.
```
- [x] generate a cert
```shell
bin/cert-generator -ca-country=china -ca-common-name=shenzhen -ca-domains=kube-system.metrics-server.svc,kube-system.coredns.svc -ca-organization=kubernetes -ca-years=50
```
```shell
--------------------------------------------------------------------------------------------
# CA Country: china
# CA Organization: kubernetes
# CA Domains: kube-system.metrics-server.svc,kube-system.coredns.svc
# Cert Path: /tmp
# Common Name: shenzhen
# Validate Years: 50 years
# CRT: /tmp/tls.crt
# Key: /tmp/tls.key
--------------------------------------------------------------------------------------------
```
Binary file added bin/cert-generator
Binary file not shown.
65 changes: 65 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2024 Hurricane1988 Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"flag"
"github.com/hurricane1988/cert-generator/pkg/certificate"
"github.com/hurricane1988/cert-generator/pkg/utils"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"strings"
)

var (
setupLog = ctrl.Log.WithName("setup")
)

func main() {
var (
country string
organization string
certPath string
validateYears int
domains string
commonName string
)
flag.StringVar(&country, "ca-country", "CN", "The country of CA, multiple items separated by ',', Default: CN.")
flag.StringVar(&organization, "ca-organization", "", "The organization of CA, multiple items separated by ','")
flag.StringVar(&domains, "ca-domains", "", "The domain of CA, multiple items separated by ','.")
flag.StringVar(&certPath, "cert-path", "/tmp", "The path to save certificate.")
flag.StringVar(&commonName, "ca-common-name", "", "The common name of CA.")
flag.IntVar(&validateYears, "ca-years", 50, "The validate years of CA, Default: 50.")
opts := zap.Options{
Development: false,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
certificate.NewCertificate(certificate.Options{
Country: strings.Split(country, ","),
CertPath: certPath,
Organization: strings.Split(organization, ","),
ValidateYears: validateYears,
Domains: strings.Split(domains, ","),
CommonName: commonName,
}).CreateCertificate()

// 打印终端
// utils.Print()
utils.Info(country, organization, domains, certPath, commonName, validateYears)
}
Loading

0 comments on commit ce3a64e

Please sign in to comment.