Skip to content

Commit

Permalink
shifted string splitting to corscfg package
Browse files Browse the repository at this point in the history
Signed-off-by: bugslayer-332 <[email protected]>
  • Loading branch information
severussnape321 committed Jul 18, 2023
1 parent 63d57c8 commit 5c13ec1
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 23 deletions.
2 changes: 2 additions & 0 deletions cmd/collector/app/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,11 @@ var otlpServerFlagsCfg = struct {
var tlsZipkinFlagsConfig = tlscfg.ServerFlagsConfig{
Prefix: "collector.zipkin",
}

var corsZipkinFlags = corscfg.Flags{
Prefix: "collector.zipkin",
}

var corsOTLPFlags = corscfg.Flags{
Prefix: "collector.otlp.http",
}
Expand Down
8 changes: 3 additions & 5 deletions cmd/collector/app/handler/otlp_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package handler
import (
"context"
"fmt"
"strings"

otlp2jaeger "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/translator/jaeger"
"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -129,11 +128,10 @@ func applyHTTPSettings(cfg *confighttp.HTTPServerSettings, opts *flags.HTTPOptio
if opts.TLS.Enabled {
cfg.TLSSetting = applyTLSSettings(&opts.TLS)
}
allowedOrigins := strings.Split(strings.ReplaceAll(opts.CORS.AllowedOrigins, " ", ""), ",")
allowedHeaders := strings.Split(strings.ReplaceAll(opts.CORS.AllowedHeaders, " ", ""), ",")

cfg.CORS = &confighttp.CORSSettings{
AllowedOrigins: allowedOrigins,
AllowedHeaders: allowedHeaders,
AllowedOrigins: opts.CORS.AllowedOrigins,
AllowedHeaders: opts.CORS.AllowedHeaders,
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/collector/app/handler/otlp_receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ func TestApplyOTLPHTTPServerSettings(t *testing.T) {
MaxVersion: "1.3",
},
CORS: corscfg.Options{
AllowedOrigins: "http://example.domain.com , http://*.domain.com ",
AllowedHeaders: "Content-Type, Accept, X-Requested-With",
AllowedOrigins: []string{"http://example.domain.com", "http://*.domain.com"},
AllowedHeaders: []string{"Content-Type", "Accept", "X-Requested-With"},
},
}

Expand Down
7 changes: 2 additions & 5 deletions cmd/collector/app/server/zipkin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package server
import (
"net"
"net/http"
"strings"
"time"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -85,13 +84,11 @@ func serveZipkin(server *http.Server, listener net.Listener, params *ZipkinServe
r := mux.NewRouter()
zHandler := zipkin.NewAPIHandler(params.Handler)
zHandler.RegisterRoutes(r)
allowedOrigins := strings.Split(strings.ReplaceAll(params.CORSConfig.AllowedOrigins, " ", ""), ",")
allowedHeaders := strings.Split(strings.ReplaceAll(params.CORSConfig.AllowedHeaders, " ", ""), ",")

cors := cors.New(cors.Options{
AllowedOrigins: allowedOrigins,
AllowedOrigins: params.CORSConfig.AllowedOrigins,
AllowedMethods: []string{"POST"}, // Allowing only POST, because that's the only handled one
AllowedHeaders: allowedHeaders,
AllowedHeaders: params.CORSConfig.AllowedHeaders,
})

recoveryHandler := recoveryhandler.NewRecoveryHandler(params.Logger, true)
Expand Down
10 changes: 8 additions & 2 deletions pkg/config/corscfg/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package corscfg

import (
"flag"
"strings"

"github.com/spf13/viper"
)
Expand All @@ -37,7 +38,12 @@ func (c Flags) AddFlags(flags *flag.FlagSet) {

func (c Flags) InitFromViper(v *viper.Viper) Options {
var p Options
p.AllowedHeaders = v.GetString(c.Prefix + corsAllowedHeaders)
p.AllowedOrigins = v.GetString(c.Prefix + corsAllowedOrigins)

allowedHeaders := v.GetString(c.Prefix + corsAllowedHeaders)
allowedOrigins := v.GetString(c.Prefix + corsAllowedOrigins)

p.AllowedOrigins = strings.Split(strings.ReplaceAll(allowedOrigins, " ", ""), ",")
p.AllowedHeaders = strings.Split(strings.ReplaceAll(allowedHeaders, " ", ""), ",")

return p
}
14 changes: 7 additions & 7 deletions pkg/config/corscfg/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package corscfg

import (
"flag"
"fmt"
"testing"

"github.com/spf13/cobra"
Expand All @@ -26,10 +27,10 @@ import (

func TestCORSFlags(t *testing.T) {
cmdFlags := []string{
"--prefix.cors.allowed-headers=Content-Type,Accept,X-Requested-With",
"--prefix.cors.allowed-origins=http://example.domain.com,http://*.domain.com",
"--prefix.cors.allowed-headers=Content-Type, Accept, X-Requested-With",
"--prefix.cors.allowed-origins=http://example.domain.com, http://*.domain.com",
}
t.Run("string slice checking", func(t *testing.T) {
t.Run("CORS Flags", func(t *testing.T) {
v := viper.New()
command := cobra.Command{}
flagSet := &flag.FlagSet{}
Expand All @@ -44,12 +45,11 @@ func TestCORSFlags(t *testing.T) {
require.NoError(t, err)

corsOpts := flagCfg.InitFromViper(v)
fmt.Println(corsOpts)

assert.Equal(t, Options{
AllowedHeaders: "Content-Type,Accept,X-Requested-With",
AllowedOrigins: "http://example.domain.com,http://*.domain.com",
AllowedHeaders: []string{"Content-Type", "Accept", "X-Requested-With"},
AllowedOrigins: []string{"http://example.domain.com", "http://*.domain.com"},
}, corsOpts)

})

}
4 changes: 2 additions & 2 deletions pkg/config/corscfg/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
package corscfg

type Options struct {
AllowedOrigins string `mapstructure:"allowed_origins"`
AllowedHeaders string `mapstructure:"allowed_headers"`
AllowedOrigins []string `mapstructure:"allowed_origins"`
AllowedHeaders []string `mapstructure:"allowed_headers"`
}

0 comments on commit 5c13ec1

Please sign in to comment.