-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable CORS settings on OTLP HTTP endpoint (#4586)
## Which problem is this PR solving? - Resolves #4459 ## Short description of the changes - Added a new package corscfg to handle the CORS requests on OTLP HTTP Endpoint and Zipkin --------- Signed-off-by: bugslayer-332 <[email protected]> Co-authored-by: bugslayer-332 <[email protected]>
- Loading branch information
1 parent
587fb74
commit 013ec4d
Showing
8 changed files
with
153 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2023 The Jaeger Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package corscfg | ||
|
||
import ( | ||
"flag" | ||
"strings" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
const ( | ||
corsPrefix = ".cors" | ||
corsAllowedHeaders = corsPrefix + ".allowed-headers" | ||
corsAllowedOrigins = corsPrefix + ".allowed-origins" | ||
) | ||
|
||
type Flags struct { | ||
Prefix string | ||
} | ||
|
||
func (c Flags) AddFlags(flags *flag.FlagSet) { | ||
flags.String(c.Prefix+corsAllowedHeaders, "", "Comma-separated CORS allowed headers. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers") | ||
flags.String(c.Prefix+corsAllowedOrigins, "", "Comma-separated CORS allowed origins. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin") | ||
} | ||
|
||
func (c Flags) InitFromViper(v *viper.Viper) Options { | ||
var p Options | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) 2023 The Jaeger Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package corscfg | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/jaegertracing/jaeger/pkg/config" | ||
) | ||
|
||
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", | ||
} | ||
t.Run("CORS Flags", func(t *testing.T) { | ||
flagCfg := Flags{ | ||
Prefix: "prefix", | ||
} | ||
v, command := config.Viperize(flagCfg.AddFlags) | ||
|
||
err := command.ParseFlags(cmdFlags) | ||
require.NoError(t, err) | ||
|
||
corsOpts := flagCfg.InitFromViper(v) | ||
fmt.Println(corsOpts) | ||
|
||
assert.Equal(t, Options{ | ||
AllowedHeaders: []string{"Content-Type", "Accept", "X-Requested-With"}, | ||
AllowedOrigins: []string{"http://example.domain.com", "http://*.domain.com"}, | ||
}, corsOpts) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) 2023 The Jaeger Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package corscfg | ||
|
||
type Options struct { | ||
AllowedOrigins []string | ||
AllowedHeaders []string | ||
} |