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

enh: add cmd to patch bastion ipblock/cidr filter #180

Merged
merged 9 commits into from
Dec 19, 2022
Prev Previous commit
Next Next commit
enh: make funcs in listpatcher public
  • Loading branch information
sven-petersen committed Dec 14, 2022
commit c9e5b4e36976f89730333ac5e0002e9d68b0ecd0
12 changes: 0 additions & 12 deletions pkg/cmd/ssh/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (
"os"
"time"

clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"sigs.k8s.io/controller-runtime/pkg/client"

gardenClient "github.com/gardener/gardenctl-v2/internal/gardenclient"
"github.com/gardener/gardenctl-v2/internal/util"
"github.com/gardener/gardenctl-v2/pkg/cmd/base"
"github.com/gardener/gardenctl-v2/pkg/target"
Expand Down Expand Up @@ -55,14 +51,6 @@ type TestUserBastionListPatcherImpl struct {
userBastionListPatcherImpl
}

func (blp *TestUserBastionListPatcherImpl) GetCurrentUser(ctx context.Context, gardenClient gardenClient.Client, authInfo *clientcmdapi.AuthInfo) (string, error) {
return blp.getCurrentUser(ctx, gardenClient, authInfo)
}

func (blp *TestUserBastionListPatcherImpl) TargetAsListOption(target target.Target) client.ListOption {
return blp.targetAsListOption(target)
}

func NewTestUserBastionPatchLister(manager target.Manager) *TestUserBastionListPatcherImpl {
target, _ := manager.CurrentTarget()
gc, _ := manager.GardenClient(target.GardenName())
Expand Down
75 changes: 31 additions & 44 deletions pkg/cmd/ssh/ssh_patch_bastionlistpatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
clientauthentication "k8s.io/client-go/pkg/apis/clientauthentication"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"sigs.k8s.io/controller-runtime/pkg/client"

gardenClient "github.com/gardener/gardenctl-v2/internal/gardenclient"
"github.com/gardener/gardenctl-v2/pkg/target"
Expand Down Expand Up @@ -73,22 +72,47 @@ func newUserBastionListPatcher(ctx context.Context, manager target.Manager) (bas
}

func (u *userBastionListPatcherImpl) List(ctx context.Context) ([]gardenoperationsv1alpha1.Bastion, error) {
authInfo, err := u.getAuthInfo(u.clientConfig)
authInfo, err := u.AuthInfo(u.clientConfig)
if err != nil {
return nil, fmt.Errorf("could not get authInfo: %w", err)
}

user, err := u.getCurrentUser(ctx, u.gardenClient, authInfo)
user, err := u.CurrentUser(ctx, u.gardenClient, authInfo)
if err != nil {
return nil, fmt.Errorf("could not get current user: %w", err)
}

listOption := u.targetAsListOption(u.target)
listOption := gardenClient.ProjectFilter{}

return u.getBastionsOfUser(ctx, user, u.gardenClient, listOption)
if u.target.ShootName() != "" {
listOption["spec.shootRef.name"] = u.target.ShootName()
}

if u.target.ProjectName() != "" {
listOption["project"] = u.target.ProjectName()
} else if u.target.SeedName() != "" {
listOption[gardencore.ShootSeedName] = u.target.SeedName()
}

var bastionsOfUser []gardenoperationsv1alpha1.Bastion

list, err := u.gardenClient.ListBastions(ctx, listOption)
if err != nil {
return nil, err
}

for _, bastion := range list.Items {
if createdBy, ok := bastion.Annotations["gardener.cloud/created-by"]; ok {
if createdBy == user {
bastionsOfUser = append(bastionsOfUser, bastion)
}
}
}

return bastionsOfUser, nil
}

func (u *userBastionListPatcherImpl) getCurrentUser(ctx context.Context, gardenClient gardenClient.Client, authInfo *clientcmdapi.AuthInfo) (string, error) {
func (u *userBastionListPatcherImpl) CurrentUser(ctx context.Context, gardenClient gardenClient.Client, authInfo *clientcmdapi.AuthInfo) (string, error) {
baseDir, err := clientcmdapi.MakeAbs(path.Dir(authInfo.LocationOfOrigin), "")
if err != nil {
return "", fmt.Errorf("Could not parse location of kubeconfig origin")
Expand Down Expand Up @@ -162,7 +186,7 @@ func (u *userBastionListPatcherImpl) getCurrentUser(ctx context.Context, gardenC
return "", fmt.Errorf("Could not detect current user")
}

func (u *userBastionListPatcherImpl) getAuthInfo(clientConfig clientcmd.ClientConfig) (*clientcmdapi.AuthInfo, error) {
func (u *userBastionListPatcherImpl) AuthInfo(clientConfig clientcmd.ClientConfig) (*clientcmdapi.AuthInfo, error) {
rawConfig, err := clientConfig.RawConfig()
if err != nil {
return nil, fmt.Errorf("could not retrieve raw config: %w", err)
Expand All @@ -181,43 +205,6 @@ func (u *userBastionListPatcherImpl) getAuthInfo(clientConfig clientcmd.ClientCo
return authInfo, nil
}

func (u *userBastionListPatcherImpl) getBastionsOfUser(ctx context.Context, user string, gardenClient gardenClient.Client, bastionListOption client.ListOption) ([]gardenoperationsv1alpha1.Bastion, error) {
var bastionsOfUser []gardenoperationsv1alpha1.Bastion

list, err := gardenClient.ListBastions(ctx, bastionListOption)
if err != nil {
return nil, err
}

for _, bastion := range list.Items {
// bastion := list.Items[i]
if createdBy, ok := bastion.Annotations["gardener.cloud/created-by"]; ok {
if createdBy == user {
bastionsOfUser = append(bastionsOfUser, bastion)
}
}
}

return bastionsOfUser, nil
}

// targetAsListOption is similiar to target.Target.AsListOption but for bastions.
func (u *userBastionListPatcherImpl) targetAsListOption(target target.Target) client.ListOption {
opt := gardenClient.ProjectFilter{}

if target.ShootName() != "" {
opt["spec.shootRef.name"] = target.ShootName()
}

if target.ProjectName() != "" {
opt["project"] = target.ProjectName()
} else if target.SeedName() != "" {
opt[gardencore.ShootSeedName] = target.SeedName()
}

return opt
}

func (u *userBastionListPatcherImpl) Patch(ctx context.Context, newBastion, oldBastion *gardenoperationsv1alpha1.Bastion) error {
return u.gardenClient.PatchBastion(ctx, newBastion, oldBastion)
}
28 changes: 3 additions & 25 deletions pkg/cmd/ssh/ssh_patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package ssh_test

import (
"context"
"fmt"
"reflect"
"time"

Expand All @@ -27,7 +26,6 @@ import (
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"

gcmocks "github.com/gardener/gardenctl-v2/internal/gardenclient/mocks"
"github.com/gardener/gardenctl-v2/internal/util"
Expand Down Expand Up @@ -401,7 +399,7 @@ var _ = Describe("SSH Patch Command", func() {
})

Describe("bastionListPatcher", func() {
Describe("GetCurrentUser", func() {
Describe("CurrentUser", func() {
var patchLister *ssh.TestUserBastionListPatcherImpl

BeforeEach(func() {
Expand All @@ -428,7 +426,7 @@ var _ = Describe("SSH Patch Command", func() {
}
gardenClient.EXPECT().CreateTokenReview(gomock.Eq(ctx), gomock.Eq(token)).Return(reviewResult, nil).Times(1)

username, err := patchLister.GetCurrentUser(ctx, gardenClient, &clientcmdapi.AuthInfo{
username, err := patchLister.CurrentUser(ctx, gardenClient, &clientcmdapi.AuthInfo{
Token: token,
})

Expand All @@ -437,32 +435,12 @@ var _ = Describe("SSH Patch Command", func() {
})

It("Should return the user when a client certificate is used", func() {
username, err := patchLister.GetCurrentUser(ctx, gardenClient, &clientcmdapi.AuthInfo{
username, err := patchLister.CurrentUser(ctx, gardenClient, &clientcmdapi.AuthInfo{
ClientCertificateData: sampleClientCertficate,
})
Expect(err).To(BeNil())
Expect(username).To(Equal(defaultUserName))
})
})

Describe("targetAsListOption", func() {
var patchLister *ssh.TestUserBastionListPatcherImpl

BeforeEach(func() {
patchLister = ssh.NewTestUserBastionPatchLister(manager)
})

It("should find bastions of current user", func() {
target := target.NewTarget(gardenName, testProject.Name, testSeed.Name, testShoot.Name)
listOption := patchLister.TargetAsListOption(target)
listOptions := &client.ListOptions{}

listOption.ApplyToList(listOptions)

selectorStr := listOptions.FieldSelector.String()
Expect(selectorStr).To(ContainSubstring(fmt.Sprintf("spec.shootRef.name=%s", testShoot.Name)))
Expect(selectorStr).To(ContainSubstring(fmt.Sprintf("project=%s", testProject.Name)))
})
})
})
})