Merge pull request #948 from hawkowl/hawkowl/repr-api

Add String() representations of some documents to aid debugging
This commit is contained in:
Angus Salkeld 2020-08-24 15:00:31 +10:00 коммит произвёл GitHub
Родитель 7a17f4ad44 e2de9d9798
Коммит 7f52c23580
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 135 добавлений и 0 удалений

Просмотреть файл

@ -11,6 +11,10 @@ type AsyncOperationDocuments struct {
AsyncOperationDocuments []*AsyncOperationDocument `json:"Documents,omitempty"`
}
func (c *AsyncOperationDocuments) String() string {
return encodeJSON(c)
}
// AsyncOperationDocument represents a asyncOperation document.
// pkg/database/cosmosdb requires its definition.
type AsyncOperationDocument struct {
@ -30,3 +34,7 @@ type AsyncOperationDocument struct {
OpenShiftClusterKey string `json:"openShiftClusterKey,omitempty"`
OpenShiftCluster *OpenShiftCluster `json:"openShiftCluster,omitempty"`
}
func (c *AsyncOperationDocument) String() string {
return encodeJSON(c)
}

Просмотреть файл

@ -11,6 +11,10 @@ type BillingDocuments struct {
BillingDocuments []*BillingDocument `json:"Documents,omitempty"`
}
func (c *BillingDocuments) String() string {
return encodeJSON(c)
}
// BillingDocument represents a billing document.
// pkg/database/cosmosdb requires its definition.
type BillingDocument struct {
@ -31,3 +35,7 @@ type BillingDocument struct {
ClusterResourceGroupIDKey string `json:"clusterResourceGroupIDKey,omitempty"`
InfraID string `json:"infraId,omitempty"`
}
func (c *BillingDocument) String() string {
return encodeJSON(c)
}

Просмотреть файл

@ -11,6 +11,10 @@ type OpenShiftClusterDocuments struct {
OpenShiftClusterDocuments []*OpenShiftClusterDocument `json:"Documents,omitempty"`
}
func (c *OpenShiftClusterDocuments) String() string {
return encodeJSON(c)
}
// OpenShiftClusterDocument represents an OpenShift cluster document.
// pkg/database/cosmosdb requires its definition.
type OpenShiftClusterDocument struct {
@ -42,3 +46,7 @@ type OpenShiftClusterDocument struct {
CorrelationData *CorrelationData `json:"correlationData,omitempty"`
}
func (c *OpenShiftClusterDocument) String() string {
return encodeJSON(c)
}

51
pkg/api/pretty.go Normal file
Просмотреть файл

@ -0,0 +1,51 @@
// stringifying representations of API documents for debugging and testing
// logging
package api
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"reflect"
"strings"
"github.com/ugorji/go/codec"
)
func newSecretPreservingJsonHandle() *codec.JsonHandle {
h := &codec.JsonHandle{
BasicHandle: codec.BasicHandle{
DecodeOptions: codec.DecodeOptions{
ErrorIfNoField: true,
},
},
}
h.SetInterfaceExt(reflect.TypeOf(SecureBytes{}), 1, secureHidingExt{})
h.SetInterfaceExt(reflect.TypeOf((*SecureString)(nil)), 1, secureHidingExt{})
return h
}
func encodeJSON(i interface{}) string {
w := &strings.Builder{}
enc := codec.NewEncoder(w, newSecretPreservingJsonHandle())
err := enc.Encode(i)
if err != nil {
return err.Error()
}
return w.String()
}
var _ codec.InterfaceExt = (*secureHidingExt)(nil)
type secureHidingExt struct {
}
func (s secureHidingExt) ConvertExt(v interface{}) interface{} {
return "[REDACTED]"
}
func (s secureHidingExt) UpdateExt(dest interface{}, v interface{}) {
panic("cannot be used to decode!")
}

52
pkg/api/pretty_test.go Normal file
Просмотреть файл

@ -0,0 +1,52 @@
// stringifying representations of API documents for debugging and testing
// logging
package api
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"fmt"
"strings"
"testing"
)
func TestSubscriptionDocumentString(t *testing.T) {
doc := &SubscriptionDocument{ID: "test"}
stringed := fmt.Sprint(doc)
if !strings.Contains(stringed, "test") {
t.Fatalf("SubscriptionDocument did not format: %s", stringed)
}
}
func TestOpenShiftClusterDocumentString(t *testing.T) {
doc := &OpenShiftClusterDocument{
ID: "test",
OpenShiftCluster: &OpenShiftCluster{
Properties: OpenShiftClusterProperties{
KubeadminPassword: SecureString("SECRET"),
},
},
}
stringed := fmt.Sprint(doc)
if !strings.Contains(stringed, "test") {
t.Fatalf("OpenShiftClusterDocument did not format: %s", stringed)
}
// no secrets should survive
if strings.Contains(stringed, "SECRET") {
t.Fatalf("OpenShiftClusterDocument did not hide secrets: %s", stringed)
}
if !strings.Contains(stringed, "[REDACTED]") {
t.Fatalf("OpenShiftClusterDocument did not hide secrets: %s", stringed)
}
}
func TestBillingDocumentString(t *testing.T) {
doc := &BillingDocument{ID: "test"}
stringed := fmt.Sprint(doc)
if !strings.Contains(stringed, "test") {
t.Fatalf("BillingDocument did not format: %s", stringed)
}
}

Просмотреть файл

@ -11,6 +11,10 @@ type SubscriptionDocuments struct {
SubscriptionDocuments []*SubscriptionDocument `json:"Documents,omitempty"`
}
func (c *SubscriptionDocuments) String() string {
return encodeJSON(c)
}
// SubscriptionDocument represents a subscription document.
// pkg/database/cosmosdb requires its definition.
type SubscriptionDocument struct {
@ -33,3 +37,7 @@ type SubscriptionDocument struct {
Subscription *Subscription `json:"subscription,omitempty"`
}
func (c *SubscriptionDocument) String() string {
return encodeJSON(c)
}