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

Spire injection client #26

Merged
merged 1 commit into from
Jul 18, 2022
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
40 changes: 31 additions & 9 deletions pkg/spire/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"time"

"github.com/pkg/errors"
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
"github.com/spiffe/go-spiffe/v2/workloadapi"
entryv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/entry/v1"
Expand All @@ -31,17 +32,44 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/rest"
"knative.dev/pkg/injection"
"knative.dev/pkg/logging"
)

func init() {
injection.Default.RegisterClient(withControllerClient)
}

// controllerKey is a way to associate the ControllerAPIClient from inside the context.Context
type controllerKey struct{}

// GetControllerAPIClient extracts the ControllerAPIClient from the context.
func GetControllerAPIClient(ctx context.Context) ControllerAPIClient {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing some testing here. Seems like I am running into the untyped == nil. Do I need to setup the context in some way for this to work?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm interesting, yea maybe it requires setting up the context with the injection library.

untyped := ctx.Value(controllerKey{})
if untyped == nil {
logging.FromContext(ctx).Errorf("Unable to fetch client from context.")
return nil
}
return untyped.(*spireControllerAPIClient)
}

func withControllerClient(ctx context.Context, cfg *rest.Config) context.Context {
return context.WithValue(ctx, controllerKey{}, &spireEntrypointerAPIClient{})
}

type spireControllerAPIClient struct {
config spireconfig.SpireConfig
config *spireconfig.SpireConfig
serverConn *grpc.ClientConn
workloadConn *workloadapi.X509Source
entryClient entryv1.EntryClient
workloadAPI *workloadapi.Client
}

func (sc *spireControllerAPIClient) setupClient(ctx context.Context) error {
if sc.config == nil {
return errors.New("config has not been set yet")
}
if sc.entryClient == nil || sc.workloadConn == nil || sc.workloadAPI == nil || sc.serverConn == nil {
return sc.dial(ctx)
}
Expand Down Expand Up @@ -85,14 +113,8 @@ func (sc *spireControllerAPIClient) dial(ctx context.Context) error {
return nil
}

// NewSpireControllerAPIClient creates a new NewSpireControllerAPIClient for the pipeline controller
func NewSpireControllerAPIClient(c spireconfig.SpireConfig) ControllerAPIClient {
if c.MockSpire {
return &MockClient{}
}
return &spireControllerAPIClient{
config: c,
}
func (sc *spireControllerAPIClient) SetConfig(c spireconfig.SpireConfig) {
sc.config = &c
}

func (sc *spireControllerAPIClient) nodeEntry(nodeName string) *spiffetypes.Entry {
Expand Down
36 changes: 30 additions & 6 deletions pkg/spire/entrypointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,41 @@ import (
"github.com/spiffe/go-spiffe/v2/svid/x509svid"
"github.com/spiffe/go-spiffe/v2/workloadapi"
spireconfig "github.com/tektoncd/pipeline/pkg/spire/config"
"k8s.io/client-go/rest"
"knative.dev/pkg/injection"
"knative.dev/pkg/logging"
)

func init() {
injection.Default.RegisterClient(withEntrypointerClient)
}

// entrypointerKey is a way to associate the EntrypointerAPIClient from inside the context.Context
type entrypointerKey struct{}

// GetEntrypointerAPIClient extracts the EntrypointerAPIClient from the context.
func GetEntrypointerAPIClient(ctx context.Context) EntrypointerAPIClient {
untyped := ctx.Value(entrypointerKey{})
if untyped == nil {
logging.FromContext(ctx).Errorf("Unable to fetch client from context.")
return nil
}
return untyped.(*spireEntrypointerAPIClient)
}

func withEntrypointerClient(ctx context.Context, cfg *rest.Config) context.Context {
return context.WithValue(ctx, entrypointerKey{}, &spireEntrypointerAPIClient{})
}

type spireEntrypointerAPIClient struct {
config spireconfig.SpireConfig
config *spireconfig.SpireConfig
client *workloadapi.Client
}

func (w *spireEntrypointerAPIClient) setupClient(ctx context.Context) error {
if w.config == nil {
return errors.New("config has not been set yet")
}
if w.client == nil {
return w.dial(ctx)
}
Expand Down Expand Up @@ -64,11 +91,8 @@ func (w *spireEntrypointerAPIClient) getxsvid(ctx context.Context) (*x509svid.SV
return nil, errors.Wrap(err, "requested SVID failed to get fetched and timed out")
}

// NewSpireEntrypointerAPIClient creates a new spireEntrypointerApiClient for the entrypointer
func NewSpireEntrypointerAPIClient(c spireconfig.SpireConfig) EntrypointerAPIClient {
return &spireEntrypointerAPIClient{
config: c,
}
func (w *spireEntrypointerAPIClient) SetConfig(c spireconfig.SpireConfig) {
w.config = &c
}

func (w *spireEntrypointerAPIClient) Close() error {
Expand Down
3 changes: 3 additions & 0 deletions pkg/spire/spire.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"context"

"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
spireconfig "github.com/tektoncd/pipeline/pkg/spire/config"
corev1 "k8s.io/api/core/v1"
)

Expand All @@ -50,11 +51,13 @@ type ControllerAPIClient interface {
CreateEntries(ctx context.Context, tr *v1beta1.TaskRun, pod *corev1.Pod, ttl int) error
DeleteEntry(ctx context.Context, tr *v1beta1.TaskRun, pod *corev1.Pod) error
VerifyTaskRunResults(ctx context.Context, prs []v1beta1.PipelineResourceResult, tr *v1beta1.TaskRun) error
SetConfig(c spireconfig.SpireConfig)
}

// EntrypointerAPIClient interface maps to the spire entrypointer API to interact with spire
type EntrypointerAPIClient interface {
Close() error
// Sign returns the signature material to be put in the PipelineResourceResult to append to the output results
Sign(ctx context.Context, results []v1beta1.PipelineResourceResult) ([]v1beta1.PipelineResourceResult, error)
SetConfig(c spireconfig.SpireConfig)
}
20 changes: 20 additions & 0 deletions pkg/spire/spire_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,25 @@ import (

"github.com/pkg/errors"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
spireconfig "github.com/tektoncd/pipeline/pkg/spire/config"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/rest"
"knative.dev/pkg/injection"
)

func init() {
injection.Fake.RegisterClient(withFakeControllerClient)
injection.Fake.RegisterClient(withFakeEntrypointerClient)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works with the mocked spire tests. But why don't you need the GetEntrypointerAPIClient here?

}

func withFakeControllerClient(ctx context.Context, cfg *rest.Config) context.Context {
return context.WithValue(ctx, controllerKey{}, &MockClient{})
}

func withFakeEntrypointerClient(ctx context.Context, cfg *rest.Config) context.Context {
return context.WithValue(ctx, entrypointerKey{}, &MockClient{})
}

// MockClient is a client used for mocking the this package for unit testing
// other tekton components that use the spire entrypointer or controller client.
//
Expand Down Expand Up @@ -207,3 +223,7 @@ func (sc *MockClient) Sign(ctx context.Context, results []v1beta1.PipelineResour
func (sc *MockClient) Close() error {
return nil
}

func (sc *MockClient) SetConfig(c spireconfig.SpireConfig) {
return
}