Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 committed Oct 27, 2021
1 parent 756954a commit 67c0dc2
Showing 1 changed file with 21 additions and 34 deletions.
55 changes: 21 additions & 34 deletions pkg/storage/registry/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,7 @@ func getProviderAddr(ctx context.Context, r rule) string {

func (b *reg) ListProviders(ctx context.Context) ([]*registrypb.ProviderInfo, error) {
providers := []*registrypb.ProviderInfo{}
for k, v := range b.c.Rules {

// check if the provider is allowed to be shown according to the
// user agent that made the request
// if the list of AllowedUserAgents is empty, this means that
// every agents that made the request could see the provider

if len(v.AllowedUserAgents) != 0 {
ua, ok := ctxpkg.ContextGetUserAgent(ctx)
if !ok {
continue
}
if !userAgentIsAllowed(ua, v.AllowedUserAgents) {
continue // skip this provider
}
}

for k, v := range rulesFilteredByUserAgent(ctx, b.c.Rules) {
if addr := getProviderAddr(ctx, v); addr != "" {
combs := generateRegexCombinations(k)
for _, c := range combs {
Expand Down Expand Up @@ -181,6 +165,24 @@ func userAgentIsAllowed(ua *ua.UserAgent, userAgents []string) bool {
return false
}

func rulesFilteredByUserAgent(ctx context.Context, rules map[string]rule) map[string]rule {
filtered := make(map[string]rule)
for prefix, rule := range rules {
// check if the provider is allowed to be shown according to the
// user agent that made the request
// if the list of AllowedUserAgents is empty, this means that
// every agents that made the request could see the provider

if len(rule.AllowedUserAgents) != 0 {
if ua, ok := ctxpkg.ContextGetUserAgent(ctx); !ok || !userAgentIsAllowed(ua, rule.AllowedUserAgents) {
continue
}
}
filtered[prefix] = rule
}
return filtered
}

func (b *reg) FindProviders(ctx context.Context, ref *provider.Reference) ([]*registrypb.ProviderInfo, error) {
// find longest match
var match *registrypb.ProviderInfo
Expand All @@ -189,7 +191,7 @@ func (b *reg) FindProviders(ctx context.Context, ref *provider.Reference) ([]*re
// If the reference has a resource id set, use it to route
if ref.ResourceId != nil {
if ref.ResourceId.StorageId != "" {
for prefix, rule := range b.c.Rules {
for prefix, rule := range rulesFilteredByUserAgent(ctx, b.c.Rules) {
addr := getProviderAddr(ctx, rule)
r, err := regexp.Compile("^" + prefix + "$")
if err != nil {
Expand All @@ -215,22 +217,7 @@ func (b *reg) FindProviders(ctx context.Context, ref *provider.Reference) ([]*re
// TODO this needs to be reevaluated once all clients query the storage registry for a list of storage providers
fn := path.Clean(ref.GetPath())
if fn != "" {
for prefix, rule := range b.c.Rules {

// check if the provider is allowed to be shown according to the
// user agent that made the request
// if the list of AllowedUserAgents is empty, this means that
// every agents that made the request could see the provider

if len(rule.AllowedUserAgents) != 0 {
ua, ok := ctxpkg.ContextGetUserAgent(ctx)
if !ok {
continue
}
if !userAgentIsAllowed(ua, rule.AllowedUserAgents) {
continue // skip this provider
}
}
for prefix, rule := range rulesFilteredByUserAgent(ctx, b.c.Rules) {

addr := getProviderAddr(ctx, rule)
r, err := regexp.Compile("^" + prefix)
Expand Down

0 comments on commit 67c0dc2

Please sign in to comment.