Skip to content

Commit

Permalink
backend: add check feature function and use that for security compati…
Browse files Browse the repository at this point in the history
…bility check
  • Loading branch information
bojand committed May 13, 2024
1 parent c2b21da commit b8fd418
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
32 changes: 17 additions & 15 deletions backend/pkg/console/endpoint_compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"context"
"fmt"

"github.com/coreos/go-semver/semver"
"github.com/redpanda-data/common-go/rpadmin"
"github.com/twmb/franz-go/pkg/kmsg"
"github.com/twmb/franz-go/pkg/kversion"

Expand Down Expand Up @@ -49,11 +49,11 @@ func (s *Service) GetEndpointCompatibility(ctx context.Context) (EndpointCompati

// Required kafka requests per API endpoint
type endpoint struct {
URL string
Method string
Requests []kmsg.Request
HasRedpandaAPI bool
MinRedpandaVersion string
URL string
Method string
Requests []kmsg.Request
HasRedpandaAPI bool
RedpandaFeature string
}
endpointRequirements := []endpoint{
{
Expand Down Expand Up @@ -120,10 +120,10 @@ func (s *Service) GetEndpointCompatibility(ctx context.Context) (EndpointCompati
HasRedpandaAPI: true,
},
{
URL: consolev1alpha1connect.SecurityServiceName,
Method: "POST",
HasRedpandaAPI: true,
MinRedpandaVersion: "24.1.1-rc5", // no v prefix
URL: consolev1alpha1connect.SecurityServiceName,
Method: "POST",
HasRedpandaAPI: true,
RedpandaFeature: "role_based_access_control",
},
}

Expand Down Expand Up @@ -152,11 +152,13 @@ func (s *Service) GetEndpointCompatibility(ctx context.Context) (EndpointCompati
if endpointReq.HasRedpandaAPI && s.redpandaSvc != nil {
endpointSupported = true

if endpointReq.MinRedpandaVersion != "" {
minV, _ := semver.NewVersion(endpointReq.MinRedpandaVersion)
rpV, _ := semver.NewVersion(s.redpandaSvc.ClusterVersion())
if minV != nil && rpV != nil {
endpointSupported = rpV.Compare(*minV) >= 0
if endpointReq.RedpandaFeature != "" {
enabled, err := s.redpandaSvc.CheckFeature(ctx,
endpointReq.RedpandaFeature,
[]rpadmin.FeatureState{rpadmin.FeatureStateActive})
// only use result if no error
if err == nil {
endpointSupported = enabled
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions backend/pkg/redpanda/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,29 @@ func (s *Service) ClusterVersion() string {
trimmed = strings.ReplaceAll(trimmed, " ", "")
return strings.TrimLeft(trimmed, "v")
}

// CheckFeature checks whether redpanda has the specified feature in the specified state.
// Multiple states can be passed to check if feature state is any one of the given states.
// For example if "active" OR "available".
func (s *Service) CheckFeature(ctx context.Context, feature string, states []adminapi.FeatureState) (bool, error) {
fr, err := s.adminClient.GetFeatures(ctx)
if err != nil {
return false, err
}

match := false
for _, f := range fr.Features {
if f.Name == feature {
for _, s := range states {
if s == f.State {
match = true
break
}
}

break
}
}

return match, nil
}

0 comments on commit b8fd418

Please sign in to comment.