Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding zap logging #47

Merged
merged 1 commit into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion test/adcs-sim/certserv/certserv.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ import (
"time"

"github.com/jetstack/cert-manager/pkg/util/pki"

zaplogfmt "github.com/sykesm/zap-logfmt"
uzap "go.uber.org/zap"
"go.uber.org/zap/zapcore"
"time"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

)

type Certserv struct {
Expand Down Expand Up @@ -50,6 +58,13 @@ var (
tmplUnauthorized = caWorkDir + "/templates/unauth.tmpl"
)

var (
//scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("adcs-sim")
version = "adcs-sim-by-djkormo"
buildTime = "2023-12-31:23:00"

)
type SimOrders struct {
reject bool
delay time.Duration
Expand Down Expand Up @@ -308,6 +323,7 @@ func (c *Certserv) CreateCertificatePem(csr *x509.CertificateRequest) ([]byte, e
Issuer: c.caCert.Issuer,
Subject: csr.Subject,
NotBefore: time.Now(),

NotAfter: time.Now().Add(365 * 24 * time.Hour), // hardcoded to one year
KeyUsage: keyUsages,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
Expand Down Expand Up @@ -449,6 +465,7 @@ func (c *Certserv) initRootCert() error {
}
}
c.currentID = uint64(id)
fmt.Printf("Startign with id = %d\n", c.currentID)
fmt.Printf("Starting with id = %d\n", c.currentID)
setupLog.Info("Configuration","workdir ", caWorkDir)
return nil
}
63 changes: 53 additions & 10 deletions test/adcs-sim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ import (
"os"
"strings"
"github.com/nokia/adcs-issuer/test/adcs-sim/certserv"
zaplogfmt "github.com/sykesm/zap-logfmt"
uzap "go.uber.org/zap"
"go.uber.org/zap/zapcore"
"time"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

)

Expand All @@ -51,42 +57,74 @@ func getEnv(key, fallback string) string {
return fallback
}

var (
//scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("adcs-sim")
version = "adcs-sim-by-djkormo"
buildTime = "2024-01-01:16:00"
//buildTime= time.Now().UTC().Format("2006-01-02 15:04:05")
)

func main() {
port := flag.Int("port", 8443, "Port to listen on")
dns := flag.String("dns", "", "Comma separated list of domains for the simulator server certificate")
ips := flag.String("ips", "", "Comma separated list of IPs for the simulator server certificate")

opts := zap.Options{
Development: false, //was true
}
opts.BindFlags(flag.CommandLine)

flag.Parse()


// based on https://sdk.operatorframework.io/docs/building-operators/golang/references/logging/

configLog := uzap.NewProductionEncoderConfig()
// changing time format to RFC3339Nano -> 2006-01-02T15:04:05.999999999Z07:00"
configLog.EncodeTime = func(ts time.Time, encoder zapcore.PrimitiveArrayEncoder) {
encoder.AppendString(ts.UTC().Format(time.RFC3339Nano))
}
logfmtEncoder := zaplogfmt.NewEncoder(configLog)

// Construct a new logr.logger.
logger := zap.New(zap.UseDevMode(false), zap.WriteTo(os.Stdout), zap.Encoder(logfmtEncoder))
ctrl.SetLogger(logger)

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

fmt.Printf("Using %s directory for simulator in main\n", caWorkDir)
fmt.Printf("Directories in %s\n",caWorkDir)
setupLog.Info("Starting ADCS Simulator", "version", version, "build time", buildTime)
setupLog.Info("Directories in in /ca","directory", caWorkDir)


files, err := ioutil.ReadDir(caWorkDir)
if err != nil {
log.Fatal(err)
}

for _, file := range files {
fmt.Println(file.Name(), file.IsDir())
setupLog.Info("Scanning directories","Directory: ", file.Name(),"is dir",file.IsDir())
}
fmt.Printf("Files in %s/ca\n",caWorkDir)
setupLog.Info("Files in /ca","directory", caWorkDir)

filesca, err := ioutil.ReadDir(caWorkDir+"/ca")
if err != nil {
log.Fatal(err)
}
for _, fileca := range filesca {
fmt.Println(fileca.Name(), fileca.IsDir())
setupLog.Info("Scanning files","File: ", fileca.Name(),"is dir",fileca.IsDir())
}

fmt.Printf("Files in %s/templates\n",caWorkDir)

setupLog.Info("Scanning files","File in directory: ", caWorkDir)
setupLog.Info("Files in /templates","directory", caWorkDir)

filestemplate, err := ioutil.ReadDir(caWorkDir+"/templates")
if err != nil {
log.Fatal(err)
}

for _, filetemplate := range filestemplate {
fmt.Println(filetemplate.Name(), filetemplate.IsDir())
setupLog.Info("Scanning files","File: ", filetemplate.Name(),"is dir",filetemplate.IsDir())
}

certserv, err := certserv.NewCertserv()
Expand All @@ -107,7 +145,10 @@ func main() {

// Generate certificate for the simulator server TLS
func generateServerCertificate(cs *certserv.Certserv, ips *string, dns *string) error {
fmt.Printf("Using %s directory for simulator in generateServerCertificate: ip: %s, dns %s \n", caWorkDir, *ips,*dns)

setupLog.Info("Configuration","workdir ", caWorkDir)
setupLog.Info("Configuration","ip ", *ips)
setupLog.Info("Configuration","dns ", *dns)
var ipAddresses []net.IP
if ips != nil && len(*ips) > 0 {
for _, ipString := range strings.Split(*ips, ",") {
Expand All @@ -128,7 +169,9 @@ func generateServerCertificate(cs *certserv.Certserv, ips *string, dns *string)
organization := []string{"ADCS simulator for cert-manager testing"}

if len(ipAddresses) == 0 && len(dnsNames) == 0 {
setupLog.Info("No subjects specified on certificate","ipAddresses", ipAddresses,"dnsNames",dnsNames)
return fmt.Errorf("no subjects specified on certificate")

}
var commonName string
if len(dnsNames) > 0 {
Expand Down Expand Up @@ -159,7 +202,7 @@ func generateServerCertificate(cs *certserv.Certserv, ips *string, dns *string)
return fmt.Errorf("error creating x509 key: %s", err.Error())
}
keyBytes := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)})
fmt.Printf("Writing server key file: %s\n", serverKey)
setupLog.Info("Writting","key file ", serverKey)
err = os.WriteFile(serverKey, keyBytes, 0644)
if err != nil {
return fmt.Errorf("error writing key file: %s", err.Error())
Expand Down
33 changes: 33 additions & 0 deletions test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,64 @@ require (
github.com/jetstack/cert-manager v1.3.1
github.com/nokia/adcs-issuer v0.0.0-20221107114751-732b348ad092
github.com/stretchr/testify v1.6.1
github.com/sykesm/zap-logfmt v0.0.4
go.uber.org/zap v1.15.0
sigs.k8s.io/controller-runtime v0.8.3
)

require (
github.com/Azure/go-ntlmssp v0.0.0-20200615164410-66371956d46c // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/evanphx/json-patch v4.9.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-logr/logr v0.3.0 // indirect
github.com/go-logr/zapr v0.2.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/google/go-cmp v0.5.2 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/googleapis/gnostic v0.5.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/imdario/mergo v0.3.10 // indirect
github.com/json-iterator/go v1.1.10 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.7.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.10.0 // indirect
github.com/prometheus/procfs v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.6.0 // indirect
go.uber.org/multierr v1.5.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
gomodules.xyz/jsonpatch/v2 v2.1.0 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
k8s.io/api v0.20.2 // indirect
k8s.io/apiextensions-apiserver v0.20.2 // indirect
k8s.io/apimachinery v0.20.2 // indirect
k8s.io/client-go v0.20.2 // indirect
k8s.io/component-base v0.20.2 // indirect
k8s.io/klog/v2 v2.4.0 // indirect
k8s.io/kube-aggregator v0.19.0 // indirect
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd // indirect
k8s.io/utils v0.0.0-20210111153108-fddb29f9d009 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.0.2 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
Expand Down
Loading
Loading