Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bitoku committed Dec 4, 2024
1 parent e3ac347 commit 59dccd3
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 17 deletions.
107 changes: 105 additions & 2 deletions pkg/cluster/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ package cluster
// Licensed under the Apache License 2.0.

import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"

Check failure on line 11 in pkg/cluster/tls.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA1019: "io/ioutil" has been deprecated since Go 1.19: As of Go 1.16, the same functionality is now provided by package [io] or package [os], and those implementations should be preferred in new code. See the specific function documentation for details. (staticcheck)
"net/http"
"net/url"
"strings"
"sync"
"time"

"github.com/Azure/go-autorest/logger"
configv1 "github.com/openshift/api/config/v1"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -25,6 +33,101 @@ const (
OneCertPublicIssuerName = "OneCertV2-PublicCA"
)

type writer struct {
*manager
logLevel logger.LevelType
}

func (w writer) Writeln(level logger.LevelType, message string) {
w.Writef(level, "%s\n", message)
}
func (w writer) Writef(level logger.LevelType, format string, a ...interface{}) {
if w.logLevel >= level {
w.log.Log(logrus.InfoLevel, entryHeader(level), fmt.Sprintf(format, a...))
}
}
func (w writer) WriteRequest(req *http.Request, filter logger.Filter) {
if req == nil {
return
}
b := &bytes.Buffer{}
fmt.Fprintf(b, "%s REQUEST: %s %s\n", entryHeader(logger.LogInfo), req.Method, processURL(filter, req.URL))
// dump headers
for k, v := range req.Header {
if ok, mv := processHeader(filter, k, v); ok {
fmt.Fprintf(b, "%s: %s\n", k, strings.Join(mv, ","))
}
}
if req.Body != nil && !strings.Contains(req.Header.Get("Content-Type"), "application/octet-stream") {
// dump body
body, err := ioutil.ReadAll(req.Body)
if err == nil {
fmt.Fprintln(b, string(processBody(filter, body)))
if nc, ok := req.Body.(io.Seeker); ok {
// rewind to the beginning
nc.Seek(0, io.SeekStart)
} else {
// recreate the body
req.Body = ioutil.NopCloser(bytes.NewReader(body))
}
} else {
fmt.Fprintf(b, "failed to read body: %v\n", err)
}
}
w.Writeln(logger.LogInfo, b.String())
}
func (w writer) WriteResponse(resp *http.Response, filter logger.Filter) {
if resp == nil {
return
}
b := &bytes.Buffer{}
fmt.Fprintf(b, "%s RESPONSE: %d %s\n", entryHeader(logger.LogInfo), resp.StatusCode, processURL(filter, resp.Request.URL))
// dump headers
for k, v := range resp.Header {
if ok, mv := processHeader(filter, k, v); ok {
fmt.Fprintf(b, "%s: %s\n", k, strings.Join(mv, ","))
}
}
if resp.Body != nil && !strings.Contains(resp.Header.Get("Content-Type"), "application/octet-stream") {
// dump body
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err == nil {
fmt.Fprintln(b, string(processBody(filter, body)))
resp.Body = ioutil.NopCloser(bytes.NewReader(body))
} else {
fmt.Fprintf(b, "failed to read body: %v\n", err)
}
}
w.Writeln(logger.LogInfo, b.String())
}

func processURL(f logger.Filter, u *url.URL) string {
if f.URL == nil {
return u.String()
}
return f.URL(u)
}

func processHeader(f logger.Filter, k string, val []string) (bool, []string) {
if f.Header == nil {
return true, val
}
return f.Header(k, val)
}

func processBody(f logger.Filter, b []byte) []byte {
if f.Body == nil {
return b
}
return f.Body(b)
}

func entryHeader(level logger.LevelType) string {
// this format provides a fixed number of digits so the size of the timestamp is constant
return fmt.Sprintf("(%s) %s:", time.Now().Format("2006-01-02T15:04:05.0000000Z07:00"), level.String())
}

func (m *manager) createCertificates(ctx context.Context) error {
managedDomain, err := dns.ManagedDomain(m.env, m.doc.OpenShiftCluster.Properties.ClusterProfile.Domain)
if err != nil {
Expand All @@ -49,6 +152,8 @@ func (m *manager) createCertificates(ctx context.Context) error {
},
}

logger.Instance = writer{manager: m, logLevel: logger.LogInfo}

for _, c := range certs {
m.log.Printf("creating certificate %s", c.certificateName)
err = m.env.ClusterKeyvault().CreateSignedCertificate(ctx, OneCertPublicIssuerName, c.certificateName, c.commonName, keyvault.EkuServerAuth)
Expand All @@ -57,8 +162,6 @@ func (m *manager) createCertificates(ctx context.Context) error {
}
}

logger.Instance = logger.NewFileLogger()

for _, c := range certs {
m.log.Printf("waiting for certificate %s", c.certificateName)
wg := sync.WaitGroup{}
Expand Down
22 changes: 7 additions & 15 deletions vendor/github.com/Azure/go-autorest/logger/logger.go

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

0 comments on commit 59dccd3

Please sign in to comment.