Skip to content

Commit

Permalink
added spire package with unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: pxp928 <parth.psu@gmail.com>
  • Loading branch information
pxp928 committed Aug 9, 2022
1 parent 369665c commit 44f265a
Show file tree
Hide file tree
Showing 226 changed files with 32,743 additions and 680 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ run:
skip-dirs:
- vendor
- pkg/client
- pkg/spire/test
timeout: 10m
modules-download-mode: vendor
12 changes: 9 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/tektoncd/pipeline
go 1.17

require (
github.com/Microsoft/go-winio v0.5.2
github.com/ahmetb/gen-crd-api-reference-docs v0.3.1-0.20220720053627-e327d0730470 // Waiting for https://github.com/ahmetb/gen-crd-api-reference-docs/pull/43/files to merge
github.com/cloudevents/sdk-go/v2 v2.10.1
github.com/containerd/containerd v1.5.13
Expand All @@ -17,11 +18,14 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/opencontainers/image-spec v1.0.3-0.20220114050600-8b9d41f48198
github.com/pkg/errors v0.9.1
github.com/spiffe/go-spiffe/v2 v2.1.1
github.com/spiffe/spire-api-sdk v1.3.1
github.com/tektoncd/plumbing v0.0.0-20220728121531-8a725746af4f
go.opencensus.io v0.23.0
go.uber.org/zap v1.21.0
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b
gomodules.xyz/jsonpatch/v2 v2.2.0
gopkg.in/square/go-jose.v2 v2.5.1
k8s.io/api v0.23.5
k8s.io/apimachinery v0.23.5
k8s.io/client-go v0.23.5
Expand All @@ -34,6 +38,8 @@ require (

require github.com/benbjohnson/clock v1.1.0 // indirect

require github.com/zeebo/errs v1.2.2 // indirect

require (
cloud.google.com/go/compute v1.5.0 // indirect
contrib.go.opencensus.io/exporter/ocagent v0.7.1-0.20200907061046-05415f1de66d // indirect
Expand Down Expand Up @@ -119,7 +125,7 @@ require (
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.0 // indirect
github.com/stretchr/testify v1.8.0
github.com/tektoncd/resolution v0.0.0-20220331203013-e4203c70c5eb
github.com/vbatts/tar-split v0.11.2 // indirect
go.uber.org/atomic v1.9.0 // indirect
Expand All @@ -139,8 +145,8 @@ require (
google.golang.org/api v0.70.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220303160752-862486edd9cc // indirect
google.golang.org/grpc v1.44.0 // indirect
google.golang.org/protobuf v1.27.1 // indirect
google.golang.org/grpc v1.46.0
google.golang.org/protobuf v1.28.0
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
19 changes: 17 additions & 2 deletions go.sum

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

19 changes: 17 additions & 2 deletions pkg/reconciler/testing/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
_ "knative.dev/pkg/client/injection/kube/informers/factory/filtered/fake"
)

// SetupFakeContext sets up the the Context and the fake filtered informers for the tests.
// SetupFakeContext sets up the Context and the fake filtered informers for the tests.
func SetupFakeContext(t *testing.T) (context.Context, []controller.Informer) {
ctx, _, informer := setupFakeContextWithLabelKey(t)
cloudEventClientBehaviour := cloudevent.FakeClientBehaviour{
Expand All @@ -31,6 +31,12 @@ func SetupFakeContext(t *testing.T) (context.Context, []controller.Informer) {
return WithLogger(ctx, t), informer
}

// SetupDefaultContext sets up the Context and the default filtered informers for the tests.
func SetupDefaultContext(t *testing.T) (context.Context, []controller.Informer) {
ctx, _, informer := setupDefaultContextWithLabelKey(t)
return WithLogger(ctx, t), informer
}

// WithLogger returns the the Logger
func WithLogger(ctx context.Context, t *testing.T) context.Context {
return logging.WithLogger(ctx, TestLogger(t))
Expand All @@ -42,7 +48,7 @@ func TestLogger(t *testing.T) *zap.SugaredLogger {
return logger.Sugar().Named(t.Name())
}

// setupFakeContextWithLabelKey sets up the the Context and the fake informers for the tests
// setupFakeContextWithLabelKey sets up the Context and the fake informers for the tests
// The provided context includes the FilteredInformerFactory LabelKey.
func setupFakeContextWithLabelKey(t zaptest.TestingT) (context.Context, context.CancelFunc, []controller.Informer) {
ctx, c := context.WithCancel(logtesting.TestContextWithLogger(t))
Expand All @@ -51,3 +57,12 @@ func setupFakeContextWithLabelKey(t zaptest.TestingT) (context.Context, context.
ctx, is := injection.Fake.SetupInformers(ctx, &rest.Config{})
return ctx, c, is
}

// setupDefaultContextWithLabelKey sets up the Context and the default informers for the tests
// The provided context includes the FilteredInformerFactory LabelKey.
func setupDefaultContextWithLabelKey(t zaptest.TestingT) (context.Context, context.CancelFunc, []controller.Informer) {
ctx, c := context.WithCancel(logtesting.TestContextWithLogger(t))
ctx = filteredinformerfactory.WithSelectors(ctx, v1beta1.ManagedByLabelKey)
ctx, is := injection.Default.SetupInformers(ctx, &rest.Config{})
return ctx, c, is
}
68 changes: 68 additions & 0 deletions pkg/spire/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2022 The Tekton 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 config

import (
"fmt"
"sort"
"strings"
)

// SpireConfig holds the images reference for a number of container images used
// across tektoncd pipelines.
type SpireConfig struct {
// The trust domain corresponds to the trust root of a SPIFFE identity provider.
TrustDomain string
// Path to the spire agent socket defined by the CSI driver
SocketPath string
// Spire server address
ServerAddr string
// Prefix to attach to the node name when registering it with the spire server
NodeAliasPrefix string

// MockSpire only to be used for testing the controller, will not exhibit
// all characteristics of spire since it is only being used in the context
// of process memory.
MockSpire bool
}

// Validate returns an error if any image is not set.
func (c SpireConfig) Validate() error {
var unset []string
for _, f := range []struct {
v, name string
}{
{c.TrustDomain, "spire-trust-domain"},
{c.SocketPath, "spire-socket-path"},
{c.ServerAddr, "spire-server-addr"},
{c.NodeAliasPrefix, "spire-node-alias-prefix"},
} {
if f.v == "" {
unset = append(unset, f.name)
}
}
if len(unset) > 0 {
sort.Strings(unset)
return fmt.Errorf("found unset spire configuration flags: %s", unset)
}

if !strings.HasPrefix(c.NodeAliasPrefix, "/") {
return fmt.Errorf("Spire node alias should start with a /")
}

return nil
}
Loading

0 comments on commit 44f265a

Please sign in to comment.