Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication processor 4/4 - Add configauth to configgrpc #1810

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package auth
package configauth

import (
"context"
Expand All @@ -21,8 +21,6 @@ import (

"google.golang.org/grpc"
"google.golang.org/grpc/metadata"

"go.opentelemetry.io/collector/config/configauth"
)

var (
Expand Down Expand Up @@ -52,8 +50,8 @@ type authenticateFunc func(context.Context, map[string][]string) (context.Contex
type unaryInterceptorFunc func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler, authenticate authenticateFunc) (interface{}, error)
type streamInterceptorFunc func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler, authenticate authenticateFunc) error

// New creates an authenticator based on the given configuration
func New(cfg configauth.Authentication) (Authenticator, error) {
// NewAuthenticator creates an authenticator based on the given configuration
func NewAuthenticator(cfg Authentication) (Authenticator, error) {
if cfg.OIDC == nil {
return nil, errNoOIDCProvided
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package auth
package configauth

import (
"context"
Expand All @@ -22,14 +22,12 @@ import (
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"

"go.opentelemetry.io/collector/config/configauth"
)

func TestNew(t *testing.T) {
func TestNewAuthenticator(t *testing.T) {
// test
p, err := New(configauth.Authentication{
OIDC: &configauth.OIDC{
p, err := NewAuthenticator(Authentication{
OIDC: &OIDC{
Audience: "some-audience",
IssuerURL: "http://example.com",
},
Expand All @@ -42,7 +40,7 @@ func TestNew(t *testing.T) {

func TestMissingOIDC(t *testing.T) {
// test
p, err := New(configauth.Authentication{})
p, err := NewAuthenticator(Authentication{})

// verify
assert.Nil(t, p)
Expand Down
25 changes: 25 additions & 0 deletions config/configauth/configauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@

package configauth

import (
"context"

"google.golang.org/grpc"
)

// Authentication defines the auth settings for the receiver
type Authentication struct {
// The attribute (header name) to look for auth data. Optional, default value: "authentication".
Expand Down Expand Up @@ -47,3 +53,22 @@ type OIDC struct {
// Optional.
GroupsClaim string `mapstructure:"groups_claim"`
}

// ToServerOptions builds a set of server options ready to be used by the gRPC server
func (a *Authentication) ToServerOptions() ([]grpc.ServerOption, error) {
auth, err := NewAuthenticator(*a)
if err != nil {
return nil, err
}

// perhaps we should use a timeout here?
// TODO: we need a hook to call auth.Close()
if err := auth.Start(context.Background()); err != nil {
return nil, err
}

return []grpc.ServerOption{
grpc.UnaryInterceptor(auth.UnaryInterceptor),
grpc.StreamInterceptor(auth.StreamInterceptor),
}, nil
}
74 changes: 74 additions & 0 deletions config/configauth/configauth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright The OpenTelemetry 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 configauth

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestToServerOptions(t *testing.T) {
// prepare
oidcServer, err := newOIDCServer()
require.NoError(t, err)
oidcServer.Start()
defer oidcServer.Close()

config := Authentication{
OIDC: &OIDC{
IssuerURL: oidcServer.URL,
Audience: "unit-test",
GroupsClaim: "memberships",
},
}

// test
opts, err := config.ToServerOptions()

// verify
assert.NoError(t, err)
assert.NotNil(t, opts)
assert.Len(t, opts, 2) // we have two interceptors
}

func TestInvalidConfigurationFailsOnToServerOptions(t *testing.T) {

for _, tt := range []struct {
cfg Authentication
}{
{
Authentication{},
},
{
Authentication{
OIDC: &OIDC{
IssuerURL: "http://oidc.acme.invalid",
Audience: "unit-test",
GroupsClaim: "memberships",
},
},
},
} {
// test
opts, err := tt.cfg.ToServerOptions()

// verify
assert.Error(t, err)
assert.Nil(t, opts)
}

}
2 changes: 1 addition & 1 deletion internal/auth/context.go → config/configauth/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package auth
package configauth

import "context"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package auth
package configauth

import (
"context"
Expand Down
15 changes: 0 additions & 15 deletions config/configauth/empty_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package auth
package configauth

import (
"context"
Expand All @@ -29,13 +29,11 @@ import (

"github.com/coreos/go-oidc"
"google.golang.org/grpc"

"go.opentelemetry.io/collector/config/configauth"
)

type oidcAuthenticator struct {
attribute string
config configauth.OIDC
config OIDC
provider *oidc.Provider
verifier *oidc.IDTokenVerifier

Expand All @@ -56,7 +54,7 @@ var (
errNotAuthenticated = errors.New("authentication didn't succeed")
)

func newOIDCAuthenticator(cfg configauth.Authentication) (*oidcAuthenticator, error) {
func newOIDCAuthenticator(cfg Authentication) (*oidcAuthenticator, error) {
if cfg.OIDC.Audience == "" {
return nil, errNoClientIDProvided
}
Expand Down Expand Up @@ -189,7 +187,7 @@ func getGroupsFromClaims(claims map[string]interface{}, groupsClaim string) ([]s
return []string{}, nil
}

func getProviderForConfig(config configauth.OIDC) (*oidc.Provider, error) {
func getProviderForConfig(config OIDC) (*oidc.Provider, error) {
t := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Expand Down
Loading