Secrets: Clean up unused code (#108366)
* Secrets: Remove unused tracectx pkg * Secrets: Remove unused assert pkg * Secrets: Remove unused encryption interface * Secrets: Remove unused encryption file/ref
This commit is contained in:
@@ -1,26 +0,0 @@
|
||||
package assert
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func True(expr bool, msg string, args ...any) {
|
||||
if !expr {
|
||||
if len(args) > 0 {
|
||||
panic(fmt.Sprintf(msg, args...))
|
||||
} else {
|
||||
panic(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ErrorIs(err1, err2 error) {
|
||||
if !errors.Is(err1, err2) {
|
||||
panic(fmt.Sprintf("expected error %T(%+v) to be %T(%+v)", err1, err1, err2, err2))
|
||||
}
|
||||
}
|
||||
|
||||
func Equal[T comparable](v1 T, v2 T, msg string) {
|
||||
True(v1 == v2, "expected %+v to equal %+v: %s", v1, v2, msg)
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
package assert
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTrue(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
require.PanicsWithValue(t, "error msg", func() {
|
||||
True(1 == 2, "error msg")
|
||||
})
|
||||
|
||||
require.PanicsWithValue(t, "error msg 1", func() {
|
||||
True(1 == 2, "error msg %d", 1)
|
||||
})
|
||||
|
||||
require.NotPanics(t, func() {
|
||||
True(true, "oops")
|
||||
})
|
||||
}
|
||||
|
||||
func TestErrorIs(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
err := errors.New("some error")
|
||||
|
||||
require.PanicsWithValue(t, "expected error *errors.errorString(other error) to be *errors.errorString(some error)", func() {
|
||||
ErrorIs(fmt.Errorf("other error"), err)
|
||||
})
|
||||
|
||||
require.NotPanics(t, func() {
|
||||
ErrorIs(err, err)
|
||||
ErrorIs(fmt.Errorf("something: %w", err), err)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEqual(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
require.PanicsWithValue(t, "expected 1 to equal 2: details", func() {
|
||||
Equal(1, 2, "details")
|
||||
})
|
||||
|
||||
require.NotPanics(t, func() {
|
||||
Equal("a", "a", "details")
|
||||
})
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
// Package encryption provides envelope encryption for secrets manager
|
||||
|
||||
// It is heavily copied from the legacy envelope encryption implementation at github.com/grafana/grafana/pkg/services/encryption.
|
||||
|
||||
package encryption
|
||||
@@ -40,8 +40,3 @@ func (id ProviderID) Kind() (string, error) {
|
||||
func KeyLabel(providerID ProviderID) string {
|
||||
return fmt.Sprintf("%s@%s", time.Now().Format("2006-01-02"), providerID)
|
||||
}
|
||||
|
||||
// BackgroundProvider should be implemented for a provider that has a task that needs to be run in the background.
|
||||
type BackgroundProvider interface {
|
||||
Run(ctx context.Context) error
|
||||
}
|
||||
|
||||
@@ -1,57 +0,0 @@
|
||||
package tracectx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
)
|
||||
|
||||
const (
|
||||
kvSeparator = "="
|
||||
pairSeparator = "#"
|
||||
)
|
||||
|
||||
func HexEncodeTraceFromContext(ctx context.Context) string {
|
||||
carrier := propagation.MapCarrier(make(map[string]string))
|
||||
|
||||
propagation.TraceContext{}.Inject(ctx, carrier)
|
||||
|
||||
// no trace in context
|
||||
if len(carrier) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
pairs := make([]string, 0, len(carrier))
|
||||
for k, v := range carrier {
|
||||
pairs = append(pairs, k+kvSeparator+v)
|
||||
}
|
||||
|
||||
return hex.EncodeToString([]byte(strings.Join(pairs, pairSeparator)))
|
||||
}
|
||||
|
||||
func HexDecodeTraceIntoContext(ctx context.Context, encoded string) (context.Context, error) {
|
||||
if encoded == "" {
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
decoded, err := hex.DecodeString(encoded)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pairs := strings.Split(string(decoded), pairSeparator)
|
||||
|
||||
carrier := make(propagation.MapCarrier, len(pairs))
|
||||
for _, pair := range pairs {
|
||||
kv := strings.SplitN(pair, kvSeparator, 2)
|
||||
if len(kv) != 2 || kv[0] == "" || kv[1] == "" {
|
||||
return nil, fmt.Errorf("invalid key-value pair: %s", pair)
|
||||
}
|
||||
carrier[kv[0]] = kv[1]
|
||||
}
|
||||
|
||||
return propagation.TraceContext{}.Extract(ctx, carrier), nil
|
||||
}
|
||||
@@ -1,88 +0,0 @@
|
||||
package tracectx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
func TestHexEncodeTraceFromContext(t *testing.T) {
|
||||
t.Run("when no trace is present in context, it returns empty string", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
encoded := HexEncodeTraceFromContext(ctx)
|
||||
require.Empty(t, encoded)
|
||||
})
|
||||
|
||||
t.Run("when trace is present in context, it returns hex-encoded string", func(t *testing.T) {
|
||||
carrier := propagation.MapCarrier{
|
||||
"traceparent": "00-446e31681d64f9dcefd947c95ef321d0-009e2f3d8ded1892-01",
|
||||
"tracestate": "first=abc1234,second=xyz7890",
|
||||
}
|
||||
ctx := propagation.TraceContext{}.Extract(context.Background(), carrier)
|
||||
|
||||
encoded := HexEncodeTraceFromContext(ctx)
|
||||
require.NotEmpty(t, encoded)
|
||||
|
||||
traceCtx, err := HexDecodeTraceIntoContext(context.Background(), encoded)
|
||||
require.NoError(t, err)
|
||||
|
||||
span := trace.SpanFromContext(traceCtx)
|
||||
require.True(t, span.SpanContext().IsValid())
|
||||
|
||||
carrier = propagation.MapCarrier(make(map[string]string))
|
||||
propagation.TraceContext{}.Inject(traceCtx, carrier)
|
||||
require.Contains(t, carrier, "traceparent")
|
||||
require.Contains(t, carrier, "tracestate")
|
||||
})
|
||||
}
|
||||
|
||||
func TestHexDecodeTraceIntoContext(t *testing.T) {
|
||||
t.Run("when encoded string is empty, it returns original context", func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
result, err := HexDecodeTraceIntoContext(ctx, "")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, ctx, result)
|
||||
})
|
||||
|
||||
t.Run("when encoded string is valid hex, it returns context with trace", func(t *testing.T) {
|
||||
encoded := hex.EncodeToString([]byte("traceparent=00-446e31681d64f9dcefd947c95ef321d0-009e2f3d8ded1892-01#tracestate=first=abc1234,second=xyz7890"))
|
||||
|
||||
ctx, err := HexDecodeTraceIntoContext(context.Background(), encoded)
|
||||
require.NoError(t, err)
|
||||
|
||||
span := trace.SpanFromContext(ctx)
|
||||
require.True(t, span.SpanContext().IsValid())
|
||||
})
|
||||
|
||||
t.Run("when encoded string has invalid hex encoding, it returns an error", func(t *testing.T) {
|
||||
invalidHex := "invalid-hex-zzz"
|
||||
|
||||
result, err := HexDecodeTraceIntoContext(context.Background(), invalidHex)
|
||||
require.Error(t, err)
|
||||
require.Nil(t, result)
|
||||
})
|
||||
|
||||
t.Run("when decoded string has invalid key-value pair format, it returns an error", func(t *testing.T) {
|
||||
// missing key
|
||||
encoded := hex.EncodeToString([]byte("00-446e31681d64f9dcefd947c95ef321d0-009e2f3d8ded1892-01"))
|
||||
|
||||
result, err := HexDecodeTraceIntoContext(context.Background(), encoded)
|
||||
require.Error(t, err)
|
||||
require.Nil(t, result)
|
||||
})
|
||||
|
||||
t.Run("when decoded string has key without value, it returns error", func(t *testing.T) {
|
||||
// missing value
|
||||
encoded := hex.EncodeToString([]byte("traceparent="))
|
||||
|
||||
result, err := HexDecodeTraceIntoContext(context.Background(), encoded)
|
||||
require.Error(t, err)
|
||||
require.Nil(t, result)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user