Skip to content

Commit

Permalink
Read subscription validation enabled or disabled from APIPolicy and p…
Browse files Browse the repository at this point in the history
…ass to enforcer
  • Loading branch information
ashera96 committed Oct 24, 2023
1 parent 6d10ce0 commit 7809186
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 50 deletions.
1 change: 1 addition & 0 deletions adapter/api/proto/wso2/discovery/api/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ message Api {
bool systemAPI = 24;
BackendJWTTokenInfo backendJWTTokenInfo = 25;
bytes apiDefinitionFile = 26;
bool subscriptionValidation = 27;
}
5 changes: 3 additions & 2 deletions adapter/internal/oasparser/config_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ func GetEnforcerAPI(adapterInternalAPI model.AdapterInternalAPI, vhost string) *
ApplicationSecurity: adapterInternalAPI.GetXWSO2ApplicationSecurity(),
// GraphQLSchema: adapterInternalAPI.GraphQLSchema,
// GraphqlComplexityInfo: adapterInternalAPI.GraphQLComplexities.Data.List,
SystemAPI: adapterInternalAPI.IsSystemAPI,
ApiDefinitionFile: adapterInternalAPI.GetAPIDefinitionFile(),
SystemAPI: adapterInternalAPI.IsSystemAPI,
ApiDefinitionFile: adapterInternalAPI.GetAPIDefinitionFile(),
SubscriptionValidation: adapterInternalAPI.GetSubscriptionValidation(),
}
}

Expand Down
11 changes: 11 additions & 0 deletions adapter/internal/oasparser/model/adapter_internal_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type AdapterInternalAPI struct {
backendJWTTokenInfo *BackendJWTTokenInfo
apiDefinitionFile []byte
apiDefinitionEndpoint string
subscriptionValidation bool
APIProperties []dpv1alpha1.Property
// GraphQLSchema string
// GraphQLComplexities GraphQLComplexityYaml
Expand Down Expand Up @@ -231,6 +232,11 @@ func (swagger *AdapterInternalAPI) GetAPIDefinitionEndpoint() string {
return swagger.apiDefinitionEndpoint
}

// GetSubscriptionValidation returns the subscription validation status.
func (swagger *AdapterInternalAPI) GetSubscriptionValidation() bool {
return swagger.subscriptionValidation
}

// GetBackendJWTTokenInfo returns the BackendJWTTokenInfo Object.
func (swagger *AdapterInternalAPI) GetBackendJWTTokenInfo() *BackendJWTTokenInfo {
return swagger.backendJWTTokenInfo
Expand Down Expand Up @@ -338,6 +344,11 @@ func (swagger *AdapterInternalAPI) SetAPIDefinitionEndpoint(endpoint string) {
swagger.apiDefinitionEndpoint = endpoint
}

// SetSubscriptionValidation sets the subscription validation status.
func (swagger *AdapterInternalAPI) SetSubscriptionValidation(subscriptionValidation bool) {
swagger.subscriptionValidation = subscriptionValidation
}

// SetName sets the name of the API
func (swagger *AdapterInternalAPI) SetName(name string) {
swagger.title = name
Expand Down
30 changes: 8 additions & 22 deletions adapter/internal/operator/controllers/dp/api_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ func (apiReconciler *APIReconciler) resolveAPIRefs(ctx context.Context, api dpv1
return nil, fmt.Errorf("error while getting httproute resource apipolicy %s in namespace : %s with API UUID : %v, %s",
apiRef.String(), namespace, string(api.ObjectMeta.UID), err.Error())
}
if apiState.InterceptorServiceMapping, apiState.BackendJWTMapping, err =
if apiState.InterceptorServiceMapping, apiState.BackendJWTMapping, apiState.SubscriptionValidation, err =
apiReconciler.getAPIPolicyChildrenRefs(ctx, apiState.APIPolicies, apiState.ResourceAPIPolicies,
api); err != nil {
return nil, fmt.Errorf("error while getting interceptor services %s in namespace : %s with API UUID : %v, %s",
return nil, fmt.Errorf("error while getting referenced policies in apipolicy %s in namespace : %s with API UUID : %v, %s",
apiRef.String(), namespace, string(api.ObjectMeta.UID), err.Error())
}
if api.Spec.DefinitionFileRef != "" {
Expand Down Expand Up @@ -699,12 +699,14 @@ func (apiReconciler *APIReconciler) getAPIPoliciesForResources(ctx context.Conte
// getAPIPolicyChildrenRefs gets all the referenced policies in apipolicy for the resolving API
// - interceptor services
// - backend JWTs
// - subscription validation
func (apiReconciler *APIReconciler) getAPIPolicyChildrenRefs(ctx context.Context,
apiPolicies, resourceAPIPolicies map[string]dpv1alpha2.APIPolicy,
api dpv1alpha1.API) (map[string]dpv1alpha1.InterceptorService, map[string]dpv1alpha1.BackendJWT, error) {
api dpv1alpha1.API) (map[string]dpv1alpha1.InterceptorService, map[string]dpv1alpha1.BackendJWT, bool, error) {
allAPIPolicies := append(maps.Values(apiPolicies), maps.Values(resourceAPIPolicies)...)
interceptorServices := make(map[string]dpv1alpha1.InterceptorService)
backendJWTs := make(map[string]dpv1alpha1.BackendJWT)
subscriptionValidation := false
for _, apiPolicy := range allAPIPolicies {
if apiPolicy.Spec.Default != nil {
if len(apiPolicy.Spec.Default.RequestInterceptors) > 0 {
Expand All @@ -714,15 +716,6 @@ func (apiReconciler *APIReconciler) getAPIPolicyChildrenRefs(ctx context.Context
interceptorServices[utils.NamespacedName(interceptorPtr).String()] = *interceptorPtr
}
}
if apiPolicy.Spec.Default.BackendJWTPolicy != nil {
backendJWTPtr := utils.GetBackendJWT(ctx, apiReconciler.client, apiPolicy.Namespace,
apiPolicy.Spec.Default.BackendJWTPolicy.Name, &api)
if backendJWTPtr != nil {
backendJWTs[utils.NamespacedName(backendJWTPtr).String()] = *backendJWTPtr
}
}
}
if apiPolicy.Spec.Default != nil {
if len(apiPolicy.Spec.Default.ResponseInterceptors) > 0 {
interceptorPtr := utils.GetInterceptorService(ctx, apiReconciler.client, apiPolicy.Namespace,
&apiPolicy.Spec.Default.ResponseInterceptors[0], &api)
Expand All @@ -737,6 +730,7 @@ func (apiReconciler *APIReconciler) getAPIPolicyChildrenRefs(ctx context.Context
backendJWTs[utils.NamespacedName(backendJWTPtr).String()] = *backendJWTPtr
}
}
subscriptionValidation = apiPolicy.Spec.Default.SubscriptionValidation
}
if apiPolicy.Spec.Override != nil {
if len(apiPolicy.Spec.Override.RequestInterceptors) > 0 {
Expand All @@ -746,15 +740,6 @@ func (apiReconciler *APIReconciler) getAPIPolicyChildrenRefs(ctx context.Context
interceptorServices[utils.NamespacedName(interceptorPtr).String()] = *interceptorPtr
}
}
if apiPolicy.Spec.Override.BackendJWTPolicy != nil {
backendJWTPtr := utils.GetBackendJWT(ctx, apiReconciler.client, apiPolicy.Namespace,
apiPolicy.Spec.Override.BackendJWTPolicy.Name, &api)
if backendJWTPtr != nil {
backendJWTs[utils.NamespacedName(backendJWTPtr).String()] = *backendJWTPtr
}
}
}
if apiPolicy.Spec.Override != nil {
if len(apiPolicy.Spec.Override.ResponseInterceptors) > 0 {
interceptorPtr := utils.GetInterceptorService(ctx, apiReconciler.client, apiPolicy.Namespace,
&apiPolicy.Spec.Override.ResponseInterceptors[0], &api)
Expand All @@ -769,9 +754,10 @@ func (apiReconciler *APIReconciler) getAPIPolicyChildrenRefs(ctx context.Context
backendJWTs[utils.NamespacedName(backendJWTPtr).String()] = *backendJWTPtr
}
}
subscriptionValidation = apiPolicy.Spec.Override.SubscriptionValidation
}
}
return interceptorServices, backendJWTs, nil
return interceptorServices, backendJWTs, subscriptionValidation, nil
}

func (apiReconciler *APIReconciler) getResolvedBackendsMapping(ctx context.Context,
Expand Down
1 change: 1 addition & 0 deletions adapter/internal/operator/synchronizer/api_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type APIState struct {
BackendJWTMapping map[string]v1alpha1.BackendJWT
APIDefinitionFile []byte
OldOrganizationID string
SubscriptionValidation bool
}

// HTTPRouteState holds the state of the deployed httpRoutes. This state is compared with
Expand Down
1 change: 1 addition & 0 deletions adapter/internal/operator/synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func GenerateAdapterInternalAPI(apiState APIState, httpRoute *HTTPRouteState, en
adapterInternalAPI.SetInfoAPICR(*apiState.APIDefinition)
adapterInternalAPI.SetAPIDefinitionFile(apiState.APIDefinitionFile)
adapterInternalAPI.SetAPIDefinitionEndpoint(apiState.APIDefinition.Spec.DefinitionPath)
adapterInternalAPI.SetSubscriptionValidation(apiState.SubscriptionValidation)
adapterInternalAPI.EnvType = envType
resourceParams := model.ResourceParams{
AuthSchemes: apiState.Authentications,
Expand Down
36 changes: 24 additions & 12 deletions adapter/pkg/discovery/api/wso2/discovery/api/api.pb.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class APIConfig {
private JWTConfigurationDto jwtConfigurationDto;
private boolean systemAPI;
private byte[] apiDefinition;
private boolean subscriptionValidation;
/**
* getApiType returns the API type. This could be one of the following.
* HTTP, WS, WEBHOOK
Expand Down Expand Up @@ -231,6 +232,14 @@ public byte[] getApiDefinition() {
return apiDefinition;
}

/**
* Returns the subscription validation status.
* @return true if subscription validation is enabled.
*/
public boolean isSubscriptionValidation() {
return subscriptionValidation;
}

public JWTConfigurationDto getJwtConfigurationDto() {
return jwtConfigurationDto;
}
Expand Down Expand Up @@ -261,6 +270,7 @@ public static class Builder {
private GraphQLSchemaDTO graphQLSchemaDTO;
private boolean systemAPI;
private byte[] apiDefinition;
private boolean subscriptionValidation;
private JWTConfigurationDto jwtConfigurationDto;
public Builder(String name) {
this.name = name;
Expand Down Expand Up @@ -392,6 +402,7 @@ public APIConfig build() {
apiConfig.systemAPI = this.systemAPI;
apiConfig.jwtConfigurationDto = this.jwtConfigurationDto;
apiConfig.apiDefinition = this.apiDefinition;
apiConfig.subscriptionValidation = this.subscriptionValidation;
return apiConfig;
}
}
Expand Down

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

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

Loading

0 comments on commit 7809186

Please sign in to comment.