From 028120495a8cd153fff83748ea871e7989a5675d Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Tue, 10 Dec 2024 18:02:31 -0500 Subject: [PATCH 01/16] feat: konnect application auth client apis --- kong/client.go | 2 + kong/konnect_application.go | 26 ++++++ kong/konnect_application_service.go | 78 +++++++++++++++++ kong/konnect_application_service_test.go | 102 +++++++++++++++++++++++ kong/zz_generated.deepcopy.go | 55 ++++++++++++ 5 files changed, 263 insertions(+) create mode 100644 kong/konnect_application.go create mode 100644 kong/konnect_application_service.go create mode 100644 kong/konnect_application_service_test.go diff --git a/kong/client.go b/kong/client.go index df98a232..6bde3fa2 100644 --- a/kong/client.go +++ b/kong/client.go @@ -67,6 +67,7 @@ type Client struct { Vaults AbstractVaultService Keys AbstractKeyService KeySets AbstractKeySetService + KonnectApplication AbstractKonnectApplicationService Licenses AbstractLicenseService FilterChains AbstractFilterChainService @@ -165,6 +166,7 @@ func NewClient(baseURL *string, client *http.Client) (*Client, error) { kong.Vaults = (*VaultService)(&kong.common) kong.Keys = (*KeyService)(&kong.common) kong.KeySets = (*KeySetService)(&kong.common) + kong.KonnectApplication = (*KonnectApplicationService)(&kong.common) kong.Licenses = (*LicenseService)(&kong.common) kong.FilterChains = (*FilterChainService)(&kong.common) diff --git a/kong/konnect_application.go b/kong/konnect_application.go new file mode 100644 index 00000000..2287f005 --- /dev/null +++ b/kong/konnect_application.go @@ -0,0 +1,26 @@ +package kong + +// KonnectApplication represents Konnect-Application-Auth +// in Kong. +// Read https://docs.konghq.com/konnect/dev-portal/applications/application-overview/ +// +k8s:deepcopy-gen=true +type KonnectApplication struct { + ID *string `json:"id"` + CreatedAt int64 `json:"created_at"` + ClientID string `json:"client_id"` + ConsumerGroups []string `json:"consumer_groups"` + Scopes []string `json:"scopes"` + AuthStrategyID *string `json:"auth_strategy_id"` + ApplicationContext *ApplicationContext `json:"application_context"` + ExhaustedScopes []string `json:"exhausted_scopes"` + Tags *[]string `json:"tags"` +} + +// ApplicationContext reprensents the application context inside the +// Konnenct-Application-Auth. +type ApplicationContext struct { + PortalID *string `json:"portal_id"` + ApplicationID *string `json:"application_id"` + DeveloperID *string `json:"developer_id"` + OrganizationID *string `json:"organization_id"` +} diff --git a/kong/konnect_application_service.go b/kong/konnect_application_service.go new file mode 100644 index 00000000..00004836 --- /dev/null +++ b/kong/konnect_application_service.go @@ -0,0 +1,78 @@ +package kong + +import ( + "context" + "encoding/json" +) + +var _ AbstractKonnectApplicationService = &KonnectApplicationService{} + +// AbstractACLService handles consumer ACL groups in Kong. +type AbstractKonnectApplicationService interface { + // Create creates a Konnect Application in Kong. + Create(ctx context.Context, key *KonnectApplication) (*KonnectApplication, error) + // List fetches list of Konnect Applications in Kong. + List(ctx context.Context, opt *ListOpt) ([]*KonnectApplication, *ListOpt, error) + // ListAll fetches all Konnect Applications in Kong. + ListAll(ctx context.Context) ([]*KonnectApplication, error) +} + +type KonnectApplicationService service + +func (k *KonnectApplicationService) Create(ctx context.Context, key *KonnectApplication) (*KonnectApplication, error) { + queryPath := "/konnect_applications" + method := "POST" + if key.ID != nil { + queryPath = queryPath + "/" + *key.ID + method = "PUT" + } + req, err := k.client.NewRequest(method, queryPath, nil, key) + if err != nil { + return nil, err + } + + var createdKey KonnectApplication + _, err = k.client.Do(ctx, req, &createdKey) + if err != nil { + return nil, err + } + return &createdKey, nil +} + +func (k *KonnectApplicationService) List(ctx context.Context, opt *ListOpt) ([]*KonnectApplication, *ListOpt, error) { + data, next, err := k.client.list(ctx, "/konnect_applications", opt) + if err != nil { + return nil, nil, err + } + var kaas []*KonnectApplication + + for _, object := range data { + b, err := object.MarshalJSON() + if err != nil { + return nil, nil, err + } + var kaa KonnectApplication + err = json.Unmarshal(b, &kaa) + if err != nil { + return nil, nil, err + } + kaas = append(kaas, &kaa) + } + + return kaas, next, nil +} + +func (k *KonnectApplicationService) ListAll(ctx context.Context) ([]*KonnectApplication, error) { + var kaa, data []*KonnectApplication + var err error + opt := &ListOpt{Size: pageSize} + + for opt != nil { + data, opt, err = k.List(ctx, opt) + if err != nil { + return nil, err + } + kaa = append(kaa, data...) + } + return kaa, nil +} diff --git a/kong/konnect_application_service_test.go b/kong/konnect_application_service_test.go new file mode 100644 index 00000000..a4268753 --- /dev/null +++ b/kong/konnect_application_service_test.go @@ -0,0 +1,102 @@ +package kong + +import ( + "testing" + "time" + + "github.com/google/uuid" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestKonnectApplicationService_Create(T *testing.T) { + RunWhenDBMode(T, "postgres") + RunWhenKong(T, ">=3.1.0") + + assert := assert.New(T) + require := require.New(T) + + client, err := NewTestClient(nil, nil) + assert.NoError(err) + assert.NotNil(client) + + var ( + clientID = uuid.NewString() + consumerGroup = []string{uuid.NewString()} + scopes = []string{"/auth"} + authStrategyID = uuid.NewString() + exhaustedScopes = []string{"/eauth"} + orgID = uuid.NewString() + developerID = uuid.NewString() + createdAt = time.Now().Unix() + ) + + kaa := &KonnectApplication{ + ID: &clientID, + ClientID: clientID, + ConsumerGroups: consumerGroup, + Scopes: scopes, + AuthStrategyID: &authStrategyID, + ExhaustedScopes: exhaustedScopes, + ApplicationContext: &ApplicationContext{ + OrganizationID: &orgID, + DeveloperID: &developerID, + }, + CreatedAt: createdAt, + } + createResponse, err := client.KonnectApplication.Create(defaultCtx, kaa) + require.NoError(err) + require.NotNil(createResponse) + require.Equal(createResponse.ClientID, clientID) + require.Equal(createResponse.CreatedAt, createdAt) + require.Equal(createResponse.ConsumerGroups, consumerGroup) + require.Equal(createResponse.Scopes, scopes) + require.Equal(createResponse.ExhaustedScopes, exhaustedScopes) +} + +func TestKonnectApplicationService_ListAll(T *testing.T) { + RunWhenDBMode(T, "postgres") + RunWhenKong(T, ">=3.1.0") + + require := require.New(T) + + client, err := NewTestClient(nil, nil) + require.NoError(err) + require.NotNil(client) + + var ( + expectedKonnectApplications = 10 + consumerGroup = []string{uuid.NewString()} + scopes = []string{"/auth"} + authStrategyID = uuid.NewString() + exhaustedScopes = []string{"/eauth"} + orgID = uuid.NewString() + developerID = uuid.NewString() + createdAt = time.Now().Unix() + ) + + kaa := &KonnectApplication{ + ConsumerGroups: consumerGroup, + Scopes: scopes, + AuthStrategyID: &authStrategyID, + ExhaustedScopes: exhaustedScopes, + ApplicationContext: &ApplicationContext{ + OrganizationID: &orgID, + DeveloperID: &developerID, + }, + CreatedAt: createdAt, + } + + for i := 0; i < expectedKonnectApplications; i++ { + clientID := uuid.NewString() + kaa.ID = &clientID + kaa.ClientID = clientID + createResponse, err := client.KonnectApplication.Create(defaultCtx, kaa) + require.NoError(err) + require.NotNil(createResponse) + } + + listKonnectApplicationResponse, err := client.KonnectApplication.ListAll(defaultCtx) + require.NoError(err) + require.Len(listKonnectApplicationResponse, expectedKonnectApplications) +} diff --git a/kong/zz_generated.deepcopy.go b/kong/zz_generated.deepcopy.go index 9552d783..90e7c892 100644 --- a/kong/zz_generated.deepcopy.go +++ b/kong/zz_generated.deepcopy.go @@ -1422,6 +1422,61 @@ func (in *KeySet) DeepCopy() *KeySet { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *KonnectApplication) DeepCopyInto(out *KonnectApplication) { + *out = *in + if in.ID != nil { + in, out := &in.ID, &out.ID + *out = new(string) + **out = **in + } + if in.ConsumerGroups != nil { + in, out := &in.ConsumerGroups, &out.ConsumerGroups + *out = make([]string, len(*in)) + copy(*out, *in) + } + if in.Scopes != nil { + in, out := &in.Scopes, &out.Scopes + *out = make([]string, len(*in)) + copy(*out, *in) + } + if in.AuthStrategyID != nil { + in, out := &in.AuthStrategyID, &out.AuthStrategyID + *out = new(string) + **out = **in + } + if in.ApplicationContext != nil { + in, out := &in.ApplicationContext, &out.ApplicationContext + *out = new(ApplicationContext) + (*in).DeepCopyInto(*out) + } + if in.ExhaustedScopes != nil { + in, out := &in.ExhaustedScopes, &out.ExhaustedScopes + *out = make([]string, len(*in)) + copy(*out, *in) + } + if in.Tags != nil { + in, out := &in.Tags, &out.Tags + *out = new([]string) + if **in != nil { + in, out := *in, *out + *out = make([]string, len(*in)) + copy(*out, *in) + } + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new KonnectApplication. +func (in *KonnectApplication) DeepCopy() *KonnectApplication { + if in == nil { + return nil + } + out := new(KonnectApplication) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *License) DeepCopyInto(out *License) { *out = *in From c11885ead387808fabb02c31d2d466adee0a00f1 Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Tue, 10 Dec 2024 18:07:12 -0500 Subject: [PATCH 02/16] fix dependency issue --- kong/konnect_application.go | 1 + kong/zz_generated.deepcopy.go | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/kong/konnect_application.go b/kong/konnect_application.go index 2287f005..e5aec0b9 100644 --- a/kong/konnect_application.go +++ b/kong/konnect_application.go @@ -18,6 +18,7 @@ type KonnectApplication struct { // ApplicationContext reprensents the application context inside the // Konnenct-Application-Auth. +// +k8s:deepcopy-gen=true type ApplicationContext struct { PortalID *string `json:"portal_id"` ApplicationID *string `json:"application_id"` diff --git a/kong/zz_generated.deepcopy.go b/kong/zz_generated.deepcopy.go index 90e7c892..5aff0f4f 100644 --- a/kong/zz_generated.deepcopy.go +++ b/kong/zz_generated.deepcopy.go @@ -204,6 +204,42 @@ func (in *Admin) DeepCopy() *Admin { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ApplicationContext) DeepCopyInto(out *ApplicationContext) { + *out = *in + if in.PortalID != nil { + in, out := &in.PortalID, &out.PortalID + *out = new(string) + **out = **in + } + if in.ApplicationID != nil { + in, out := &in.ApplicationID, &out.ApplicationID + *out = new(string) + **out = **in + } + if in.DeveloperID != nil { + in, out := &in.DeveloperID, &out.DeveloperID + *out = new(string) + **out = **in + } + if in.OrganizationID != nil { + in, out := &in.OrganizationID, &out.OrganizationID + *out = new(string) + **out = **in + } + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ApplicationContext. +func (in *ApplicationContext) DeepCopy() *ApplicationContext { + if in == nil { + return nil + } + out := new(ApplicationContext) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *BasicAuth) DeepCopyInto(out *BasicAuth) { *out = *in From dcc44b31fb90e903eaa0885534e03bbcdfdc42fd Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Tue, 10 Dec 2024 18:11:03 -0500 Subject: [PATCH 03/16] update test to only run on the ee version of the kong --- kong/konnect_application_service_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kong/konnect_application_service_test.go b/kong/konnect_application_service_test.go index a4268753..6c91fdfa 100644 --- a/kong/konnect_application_service_test.go +++ b/kong/konnect_application_service_test.go @@ -56,7 +56,7 @@ func TestKonnectApplicationService_Create(T *testing.T) { func TestKonnectApplicationService_ListAll(T *testing.T) { RunWhenDBMode(T, "postgres") - RunWhenKong(T, ">=3.1.0") + RunWhenEnterprise(T, ">=3.1.0", RequiredFeatures{}) require := require.New(T) From de8ea215b33453ad8574dc0e7df1af708e24b7a5 Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Wed, 11 Dec 2024 10:39:14 -0500 Subject: [PATCH 04/16] delete endpoint --- kong/konnect_application_service.go | 17 +++++++++++++++++ kong/konnect_application_service_test.go | 3 +++ 2 files changed, 20 insertions(+) diff --git a/kong/konnect_application_service.go b/kong/konnect_application_service.go index 00004836..e52e7fbd 100644 --- a/kong/konnect_application_service.go +++ b/kong/konnect_application_service.go @@ -3,6 +3,7 @@ package kong import ( "context" "encoding/json" + "fmt" ) var _ AbstractKonnectApplicationService = &KonnectApplicationService{} @@ -15,6 +16,8 @@ type AbstractKonnectApplicationService interface { List(ctx context.Context, opt *ListOpt) ([]*KonnectApplication, *ListOpt, error) // ListAll fetches all Konnect Applications in Kong. ListAll(ctx context.Context) ([]*KonnectApplication, error) + // Delete deletes a Konnect Application in Kong by ID. + Delete(ctx context.Context, ID *string) error } type KonnectApplicationService service @@ -76,3 +79,17 @@ func (k *KonnectApplicationService) ListAll(ctx context.Context) ([]*KonnectAppl } return kaa, nil } + +func (k *KonnectApplicationService) Delete(ctx context.Context, ID *string) error { + if isEmptyString(ID) { + return fmt.Errorf("ID cannot be nil for Delete operation") + } + + req, err := k.client.NewRequest("DELETE", fmt.Sprintf("/konnect_applications/%s", *ID), nil, nil) + if err != nil { + return err + } + + _, err = k.client.Do(ctx, req, nil) + return err +} diff --git a/kong/konnect_application_service_test.go b/kong/konnect_application_service_test.go index 6c91fdfa..860e834d 100644 --- a/kong/konnect_application_service_test.go +++ b/kong/konnect_application_service_test.go @@ -52,6 +52,9 @@ func TestKonnectApplicationService_Create(T *testing.T) { require.Equal(createResponse.ConsumerGroups, consumerGroup) require.Equal(createResponse.Scopes, scopes) require.Equal(createResponse.ExhaustedScopes, exhaustedScopes) + + err = client.KonnectApplication.Delete(defaultCtx, createResponse.ID) + require.NoError(err) } func TestKonnectApplicationService_ListAll(T *testing.T) { From 079115a6136b17788fac526444946fd10b257b88 Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Wed, 11 Dec 2024 10:48:47 -0500 Subject: [PATCH 05/16] update kong version in test --- kong/konnect_application_service_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kong/konnect_application_service_test.go b/kong/konnect_application_service_test.go index 860e834d..f13ed03c 100644 --- a/kong/konnect_application_service_test.go +++ b/kong/konnect_application_service_test.go @@ -11,7 +11,7 @@ import ( func TestKonnectApplicationService_Create(T *testing.T) { RunWhenDBMode(T, "postgres") - RunWhenKong(T, ">=3.1.0") + RunWhenKong(T, ">=3.6.0") assert := assert.New(T) require := require.New(T) @@ -59,7 +59,7 @@ func TestKonnectApplicationService_Create(T *testing.T) { func TestKonnectApplicationService_ListAll(T *testing.T) { RunWhenDBMode(T, "postgres") - RunWhenEnterprise(T, ">=3.1.0", RequiredFeatures{}) + RunWhenEnterprise(T, ">=3.6.0", RequiredFeatures{}) require := require.New(T) From 78f5c554d6ed4f5f47d1e5c3d21cce51d0e13089 Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Wed, 11 Dec 2024 10:53:11 -0500 Subject: [PATCH 06/16] update create test to run only on the enterprise versions --- kong/konnect_application_service_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kong/konnect_application_service_test.go b/kong/konnect_application_service_test.go index f13ed03c..3032a389 100644 --- a/kong/konnect_application_service_test.go +++ b/kong/konnect_application_service_test.go @@ -11,7 +11,7 @@ import ( func TestKonnectApplicationService_Create(T *testing.T) { RunWhenDBMode(T, "postgres") - RunWhenKong(T, ">=3.6.0") + RunWhenEnterprise(T, ">=3.6.0", RequiredFeatures{}) assert := assert.New(T) require := require.New(T) From f45b798b63610b9c5ee21562eeb72ef28b292256 Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Wed, 11 Dec 2024 10:58:20 -0500 Subject: [PATCH 07/16] add missing function comments --- kong/konnect_application_service.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kong/konnect_application_service.go b/kong/konnect_application_service.go index e52e7fbd..0566612a 100644 --- a/kong/konnect_application_service.go +++ b/kong/konnect_application_service.go @@ -22,6 +22,7 @@ type AbstractKonnectApplicationService interface { type KonnectApplicationService service +// Create creates a Konnect Application in Kong. func (k *KonnectApplicationService) Create(ctx context.Context, key *KonnectApplication) (*KonnectApplication, error) { queryPath := "/konnect_applications" method := "POST" @@ -42,6 +43,7 @@ func (k *KonnectApplicationService) Create(ctx context.Context, key *KonnectAppl return &createdKey, nil } +// List fetches list of Konnect Applications in Kong. func (k *KonnectApplicationService) List(ctx context.Context, opt *ListOpt) ([]*KonnectApplication, *ListOpt, error) { data, next, err := k.client.list(ctx, "/konnect_applications", opt) if err != nil { @@ -65,6 +67,7 @@ func (k *KonnectApplicationService) List(ctx context.Context, opt *ListOpt) ([]* return kaas, next, nil } +// ListAll fetches all Konnect Applications in Kong. func (k *KonnectApplicationService) ListAll(ctx context.Context) ([]*KonnectApplication, error) { var kaa, data []*KonnectApplication var err error @@ -80,6 +83,7 @@ func (k *KonnectApplicationService) ListAll(ctx context.Context) ([]*KonnectAppl return kaa, nil } +// Delete deletes a Konnect Application in Kong by ID. func (k *KonnectApplicationService) Delete(ctx context.Context, ID *string) error { if isEmptyString(ID) { return fmt.Errorf("ID cannot be nil for Delete operation") From 88f58b427b66f465784f2c8f1a2aaa6f9d1053cc Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Wed, 11 Dec 2024 12:19:47 -0500 Subject: [PATCH 08/16] Update kong/konnect_application_service_test.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Patryk Małek --- kong/konnect_application_service_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kong/konnect_application_service_test.go b/kong/konnect_application_service_test.go index 3032a389..1fd561d5 100644 --- a/kong/konnect_application_service_test.go +++ b/kong/konnect_application_service_test.go @@ -97,6 +97,10 @@ func TestKonnectApplicationService_ListAll(T *testing.T) { createResponse, err := client.KonnectApplication.Create(defaultCtx, kaa) require.NoError(err) require.NotNil(createResponse) + + t.Cleanup(func() { + assert.NoError(client.KonnectApplication.Delete(context.Background(), createResponse.ID)) + }) } listKonnectApplicationResponse, err := client.KonnectApplication.ListAll(defaultCtx) From f7da8b27cac27f93991adc90af048f5064dc5f0d Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Wed, 11 Dec 2024 12:20:18 -0500 Subject: [PATCH 09/16] Update kong/konnect_application_service.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Patryk Małek --- kong/konnect_application_service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kong/konnect_application_service.go b/kong/konnect_application_service.go index 0566612a..34c85426 100644 --- a/kong/konnect_application_service.go +++ b/kong/konnect_application_service.go @@ -8,7 +8,7 @@ import ( var _ AbstractKonnectApplicationService = &KonnectApplicationService{} -// AbstractACLService handles consumer ACL groups in Kong. +// AbstractKonnectApplicationService handles Konnect applications in Kong. type AbstractKonnectApplicationService interface { // Create creates a Konnect Application in Kong. Create(ctx context.Context, key *KonnectApplication) (*KonnectApplication, error) From 640c0b4d803f814389040de2f7e8a949c276001c Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Wed, 11 Dec 2024 12:20:25 -0500 Subject: [PATCH 10/16] Update kong/konnect_application.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Patryk Małek --- kong/konnect_application.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kong/konnect_application.go b/kong/konnect_application.go index e5aec0b9..07359b00 100644 --- a/kong/konnect_application.go +++ b/kong/konnect_application.go @@ -16,7 +16,7 @@ type KonnectApplication struct { Tags *[]string `json:"tags"` } -// ApplicationContext reprensents the application context inside the +// ApplicationContext represents the application context inside the // Konnenct-Application-Auth. // +k8s:deepcopy-gen=true type ApplicationContext struct { From 7b6a0e845967254880ae8e50c63a64905725fa1b Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Wed, 11 Dec 2024 12:20:55 -0500 Subject: [PATCH 11/16] Update kong/konnect_application_service_test.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Patryk Małek --- kong/konnect_application_service_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/kong/konnect_application_service_test.go b/kong/konnect_application_service_test.go index 1fd561d5..109245a1 100644 --- a/kong/konnect_application_service_test.go +++ b/kong/konnect_application_service_test.go @@ -47,14 +47,16 @@ func TestKonnectApplicationService_Create(T *testing.T) { createResponse, err := client.KonnectApplication.Create(defaultCtx, kaa) require.NoError(err) require.NotNil(createResponse) + + t.Cleanup(func() { + assert.NoError(client.KonnectApplication.Delete(context.Background(), createResponse.ID)) + }) + require.Equal(createResponse.ClientID, clientID) require.Equal(createResponse.CreatedAt, createdAt) require.Equal(createResponse.ConsumerGroups, consumerGroup) require.Equal(createResponse.Scopes, scopes) require.Equal(createResponse.ExhaustedScopes, exhaustedScopes) - - err = client.KonnectApplication.Delete(defaultCtx, createResponse.ID) - require.NoError(err) } func TestKonnectApplicationService_ListAll(T *testing.T) { From acaae3200e5f32e8045ad815ff7499386ce55152 Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Wed, 11 Dec 2024 13:42:10 -0500 Subject: [PATCH 12/16] fix syntax errors --- kong/konnect_application_service_test.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/kong/konnect_application_service_test.go b/kong/konnect_application_service_test.go index 109245a1..abf5102f 100644 --- a/kong/konnect_application_service_test.go +++ b/kong/konnect_application_service_test.go @@ -1,6 +1,7 @@ package kong import ( + "context" "testing" "time" @@ -47,11 +48,11 @@ func TestKonnectApplicationService_Create(T *testing.T) { createResponse, err := client.KonnectApplication.Create(defaultCtx, kaa) require.NoError(err) require.NotNil(createResponse) - - t.Cleanup(func() { + + T.Cleanup(func() { assert.NoError(client.KonnectApplication.Delete(context.Background(), createResponse.ID)) }) - + require.Equal(createResponse.ClientID, clientID) require.Equal(createResponse.CreatedAt, createdAt) require.Equal(createResponse.ConsumerGroups, consumerGroup) @@ -63,6 +64,7 @@ func TestKonnectApplicationService_ListAll(T *testing.T) { RunWhenDBMode(T, "postgres") RunWhenEnterprise(T, ">=3.6.0", RequiredFeatures{}) + assert := assert.New(T) require := require.New(T) client, err := NewTestClient(nil, nil) @@ -99,10 +101,10 @@ func TestKonnectApplicationService_ListAll(T *testing.T) { createResponse, err := client.KonnectApplication.Create(defaultCtx, kaa) require.NoError(err) require.NotNil(createResponse) - - t.Cleanup(func() { + + T.Cleanup(func() { assert.NoError(client.KonnectApplication.Delete(context.Background(), createResponse.ID)) - }) + }) } listKonnectApplicationResponse, err := client.KonnectApplication.ListAll(defaultCtx) From 9ee4ac9a0987391ed9f8e1f9a16609b34eb5eb0b Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Wed, 11 Dec 2024 15:23:12 -0500 Subject: [PATCH 13/16] add test for list --- go.mod | 1 + go.sum | 2 + kong/konnect_application_service_test.go | 90 ++++++++++++++++++++++++ 3 files changed, 93 insertions(+) diff --git a/go.mod b/go.mod index dceebc4f..8f23917d 100644 --- a/go.mod +++ b/go.mod @@ -40,6 +40,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/samber/lo v1.47.0 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.0 // indirect diff --git a/go.sum b/go.sum index d2306b44..2b200d7d 100644 --- a/go.sum +++ b/go.sum @@ -58,6 +58,8 @@ github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk= github.com/onsi/gomega v1.33.1/go.mod h1:U4R44UsT+9eLIaYRB2a5qajjtQYn0hauxvRm16AVYg0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc= +github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/kong/konnect_application_service_test.go b/kong/konnect_application_service_test.go index abf5102f..663e8ef4 100644 --- a/kong/konnect_application_service_test.go +++ b/kong/konnect_application_service_test.go @@ -6,6 +6,7 @@ import ( "time" "github.com/google/uuid" + "github.com/samber/lo" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -111,3 +112,92 @@ func TestKonnectApplicationService_ListAll(T *testing.T) { require.NoError(err) require.Len(listKonnectApplicationResponse, expectedKonnectApplications) } + +func TestKonnectApplicationService_List(T *testing.T) { + RunWhenDBMode(T, "postgres") + RunWhenEnterprise(T, ">=3.6.0", RequiredFeatures{}) + + assert := assert.New(T) + require := require.New(T) + + client, err := NewTestClient(nil, nil) + require.NoError(err) + require.NotNil(client) + + var ( + expectedKonnectApplications = 10 + consumerGroup = []string{uuid.NewString()} + scopes = []string{"/auth"} + authStrategyID = uuid.NewString() + exhaustedScopes = []string{"/eauth"} + orgID = uuid.NewString() + developerID = uuid.NewString() + createdAt = time.Now().Unix() + tagA = "list1" + tagB = "list2" + ) + + kaa := &KonnectApplication{ + ConsumerGroups: consumerGroup, + Scopes: scopes, + AuthStrategyID: &authStrategyID, + ExhaustedScopes: exhaustedScopes, + ApplicationContext: &ApplicationContext{ + OrganizationID: &orgID, + DeveloperID: &developerID, + }, + CreatedAt: createdAt, + } + + for i := 0; i < expectedKonnectApplications/2; i++ { + clientID := uuid.NewString() + kaa.ID = &clientID + kaa.ClientID = clientID + kaa.Tags = lo.ToPtr([]string{tagA}) + createResponse, err := client.KonnectApplication.Create(defaultCtx, kaa) + require.NoError(err) + require.NotNil(createResponse) + + T.Cleanup(func() { + assert.NoError(client.KonnectApplication.Delete(context.Background(), createResponse.ID)) + }) + } + + for i := 0; i < expectedKonnectApplications/2; i++ { + clientID := uuid.NewString() + kaa.ID = &clientID + kaa.ClientID = clientID + kaa.Tags = lo.ToPtr([]string{tagB}) + createResponse, err := client.KonnectApplication.Create(defaultCtx, kaa) + require.NoError(err) + require.NotNil(createResponse) + + T.Cleanup(func() { + assert.NoError(client.KonnectApplication.Delete(context.Background(), createResponse.ID)) + }) + } + + // Filter by tag listA + listKonnectApplicationResponseByTagA, _, err := client.KonnectApplication.List(defaultCtx, &ListOpt{ + Size: 10, + Tags: []*string{lo.ToPtr(tagA)}, + }) + require.NoError(err) + require.Len(listKonnectApplicationResponseByTagA, 5) + + // Filter by tag listB + listKonnectApplicationResponseByTagB, _, err := client.KonnectApplication.List(defaultCtx, &ListOpt{ + Size: 10, + Tags: []*string{lo.ToPtr(tagB)}, + }) + require.NoError(err) + require.Len(listKonnectApplicationResponseByTagB, 5) + + size := 2 + // Filter by size + listKonnectApplicationResponseBySize, _, err := client.KonnectApplication.List(defaultCtx, &ListOpt{ + Size: size, + }) + require.NoError(err) + require.Len(listKonnectApplicationResponseBySize, size) +} From a078e4a1b5c43ac1dd26a53219f3e46713dec4a9 Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Wed, 11 Dec 2024 15:30:45 -0500 Subject: [PATCH 14/16] add changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 513ba2bc..21328fdb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -75,6 +75,14 @@ - [0.2.0](#020) - [0.1.0](#010) +## [v0.62.0] + +> Release date: 2024/12/11 + +- Added Client APIs (Create, List, ListALL, Delete) to interact + with the Konnect Application Auth resources. + [#490](https://github.com/Kong/go-kong/pull/490) + ## [v0.61.0] > Release date: 2024/12/06 From 87102938343dab4e0769a70b586f326fd11db8e9 Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Thu, 12 Dec 2024 08:03:03 -0500 Subject: [PATCH 15/16] update to require instead of assert --- kong/konnect_application_service_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kong/konnect_application_service_test.go b/kong/konnect_application_service_test.go index 663e8ef4..238ea2da 100644 --- a/kong/konnect_application_service_test.go +++ b/kong/konnect_application_service_test.go @@ -19,8 +19,8 @@ func TestKonnectApplicationService_Create(T *testing.T) { require := require.New(T) client, err := NewTestClient(nil, nil) - assert.NoError(err) - assert.NotNil(client) + require.NoError(err) + require.NotNil(client) var ( clientID = uuid.NewString() From 2b749cecb2c3dafb46a3874a680dc15b56b65028 Mon Sep 17 00:00:00 2001 From: Jay Shah Date: Thu, 12 Dec 2024 08:08:50 -0500 Subject: [PATCH 16/16] check for the status code when deleting --- kong/konnect_application_service.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/kong/konnect_application_service.go b/kong/konnect_application_service.go index 34c85426..469ea5b0 100644 --- a/kong/konnect_application_service.go +++ b/kong/konnect_application_service.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "net/http" ) var _ AbstractKonnectApplicationService = &KonnectApplicationService{} @@ -94,6 +95,11 @@ func (k *KonnectApplicationService) Delete(ctx context.Context, ID *string) erro return err } - _, err = k.client.Do(ctx, req, nil) + resp, err := k.client.Do(ctx, req, nil) + if resp.StatusCode != http.StatusNoContent { + return fmt.Errorf("failed to delete Konnect Application: %s: "+ + "expected status %v, but received %v", *ID, http.StatusNoContent, resp.Status) + } + return err }