Skip to content

Commit

Permalink
Send auth headers (#370)
Browse files Browse the repository at this point in the history
Mostly plumbing
  • Loading branch information
phliar committed Oct 24, 2018
1 parent 5265fda commit 8ab04ef
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 52 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Changelog

## v3.3.1 (unreleased)
- CLI: Send Auth header
- Library: send CallerName in header

## v3.3.0 (2018-10-04)
- Switch to gofrs/uuid (#361)
- Cleanup EnsureValidRangeConditions (#364)
Expand Down
34 changes: 34 additions & 0 deletions cmd/dosa/headers.go
@@ -0,0 +1,34 @@
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package main

import (
"fmt"
"os"
"strings"
)

// Returns the set of headers required for auth: currently only X-Auth-Params-Email.
func getAuthHeaders() map[string]string {
return map[string]string{
"X-Auth-Params-Email": fmt.Sprintf("%s@uber.com", strings.ToLower(os.Getenv("USER"))),
}
}
19 changes: 10 additions & 9 deletions cmd/dosa/main.go
Expand Up @@ -23,10 +23,11 @@ package main
import (
"bytes"
"fmt"
"github.com/jessevdk/go-flags"
"github.com/uber-go/dosa"
"os"
"os/exec"

"github.com/jessevdk/go-flags"
"github.com/uber-go/dosa"
)

// for testing, we make exit an overridable routine
Expand All @@ -40,11 +41,11 @@ type queryClientProvider func(opts GlobalOptions, scope, prefix, path, structNam

// these are overridden at build-time w/ the -ldflags -X option
var (
version = "0.0.0"
githash = "master"
timestamp = "now"
version = "0.0.0"
githash = "master"
timestamp = "now"
javaclientVersion = "1.1.0-beta"
javaclient = os.Getenv("HOME") + "/.m2/target/dependency/java-client-" + javaclientVersion + ".jar"
javaclient = os.Getenv("HOME") + "/.m2/target/dependency/java-client-" + javaclientVersion + ".jar"
)

// BuildInfo reports information about the binary build environment
Expand All @@ -70,7 +71,7 @@ type GlobalOptions struct {
Host string `long:"host" default:"127.0.0.1" description:"The hostname or IP for the gateway."`
Port string `short:"p" long:"port" default:"21300" description:"The hostname or IP for the gateway."`
ServiceName string `short:"s" long:"service" default:"dosa-gateway" description:"The TChannel service name for the gateway."`
CallerName callerFlag `long:"caller" default:"dosacli-$USER" description:"Caller will override the default caller name (which is dosacli-$USER)."`
CallerName callerFlag `long:"caller" default:"dosacli-$USER" description:"The RPC Caller name."`
Timeout timeFlag `long:"timeout" default:"60s" description:"The timeout for gateway requests. E.g., 100ms, 0.5s, 1s. If no unit is specified, milliseconds are assumed."`
Version bool `long:"version" description:"Display version info"`
}
Expand Down Expand Up @@ -131,8 +132,8 @@ dosa manages your schema both in production and development scopes`

func downloadJar() {
fmt.Println("Downloading required dependencies... This may take some time...")
cmd := exec.Command( "mvn", "org.apache.maven.plugins:maven-dependency-plugin:RELEASE:copy",
"-Dartifact=com.uber.dosa:java-client:" + javaclientVersion, "-Dproject.basedir=" + os.Getenv("HOME") + "/.m2/")
cmd := exec.Command("mvn", "org.apache.maven.plugins:maven-dependency-plugin:RELEASE:copy",
"-Dartifact=com.uber.dosa:java-client:"+javaclientVersion, "-Dproject.basedir="+os.Getenv("HOME")+"/.m2/")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
Expand Down
27 changes: 15 additions & 12 deletions cmd/dosa/options.go
Expand Up @@ -97,10 +97,11 @@ func provideAdminClient(opts GlobalOptions) (dosa.AdminClient, error) {
}

ycfg := yarpc.Config{
Host: opts.Host,
Port: opts.Port,
CallerName: opts.CallerName.String(),
ServiceName: opts.ServiceName,
Host: opts.Host,
Port: opts.Port,
CallerName: opts.CallerName.String(),
ServiceName: opts.ServiceName,
ExtraHeaders: getAuthHeaders(),
}

conn, err := yarpc.NewConnector(ycfg)
Expand All @@ -126,10 +127,11 @@ func provideMDClient(opts GlobalOptions) (c dosa.Client, err error) {
Scope: "production",
NamePrefix: "dosa_scopes_metadata",
Yarpc: yarpc.Config{
Host: opts.Host,
Port: opts.Port,
CallerName: opts.CallerName.String(),
ServiceName: opts.ServiceName,
Host: opts.Host,
Port: opts.Port,
CallerName: opts.CallerName.String(),
ServiceName: opts.ServiceName,
ExtraHeaders: getAuthHeaders(),
},
}

Expand Down Expand Up @@ -160,10 +162,11 @@ func provideShellQueryClient(opts GlobalOptions, scope, prefix, path, structName
}

ycfg := yarpc.Config{
Host: opts.Host,
Port: opts.Port,
CallerName: opts.CallerName.String(),
ServiceName: opts.ServiceName,
Host: opts.Host,
Port: opts.Port,
CallerName: opts.CallerName.String(),
ServiceName: opts.ServiceName,
ExtraHeaders: getAuthHeaders(),
}

conn, err := yarpc.NewConnector(ycfg)
Expand Down
2 changes: 1 addition & 1 deletion cmd/dosa/query.go
Expand Up @@ -57,7 +57,7 @@ type QueryCmd struct {
Prefix string `short:"p" long:"prefix" description:"Name prefix for schema types." hidden:"true"`
Path string `long:"path" description:"Path to source." required:"true"`
JarPath string `short:"j" long:"jarpath" description:"Path of the jar. This jar contains schema entities."`
ClassNames []string `short:"c" long:"classnames" description:"Classes contain schema."`
ClassNames []string `short:"c" long:"classnames" description:"Classes contain schema."`
provideClient queryClientProvider
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/dosa/schema.go
Expand Up @@ -78,7 +78,7 @@ type SchemaCmd struct {
NamePrefix string `short:"n" long:"namePrefix" description:"Name prefix for schema types."`
Prefix string `short:"p" long:"prefix" description:"Name prefix for schema types." hidden:"true"`
JarPath string `short:"j" long:"jarpath" description:"Path of the jar. This jar contains schema entities."`
ClassNames []string `short:"c" long:"classnames" description:"Classes contain schema."`
ClassNames []string `short:"c" long:"classnames" description:"Classes contain schema."`
provideClient adminClientProvider
}

Expand Down Expand Up @@ -301,8 +301,8 @@ func (c *SchemaStatus) Execute(args []string) error {
// SchemaDump contains data for executing the schema dump command
type SchemaDump struct {
*SchemaOptions
Format string `long:"format" short:"f" description:"output format" choice:"cql" choice:"uql" choice:"avro" default:"cql"`
JarPath string `short:"j" long:"jarpath" description:"Path of the jar. This jar contains schema entities."`
Format string `long:"format" short:"f" description:"output format" choice:"cql" choice:"uql" choice:"avro" default:"cql"`
JarPath string `short:"j" long:"jarpath" description:"Path of the jar. This jar contains schema entities."`
ClassNames []string `short:"c" long:"classnames" description:"Classes contain schema."`
Args struct {
Paths []string `positional-arg-name:"paths"`
Expand Down Expand Up @@ -388,7 +388,7 @@ func (c *SchemaDump) doSchemaDumpInJavaClient() {
args = append(args, element)
}
}

if c.Verbose {
args = append(args, "-v")
}
Expand Down
2 changes: 1 addition & 1 deletion connectors/memory/memory.go
Expand Up @@ -28,8 +28,8 @@ import (
"sync"
"time"

"github.com/pkg/errors"
"github.com/gofrs/uuid"
"github.com/pkg/errors"
"github.com/uber-go/dosa"
"github.com/uber-go/dosa/connectors/base"
"github.com/uber-go/dosa/encoding"
Expand Down
11 changes: 8 additions & 3 deletions connectors/yarpc/helpers.go
Expand Up @@ -415,7 +415,12 @@ func multiKeyValuesToRPCValues(keysSlice []map[string]dosa.FieldValue) ([]dosarp
return rpcFieldsSlice, nil
}

// VersionHeader returns the rpc style version header
func VersionHeader() rpc.CallOption {
return rpc.WithHeader(_version, dosa.VERSION)
// getHeaders converts the provided headers into rpc.CallOption values. A header for Version is also added.
func getHeaders(headers map[string]string) []rpc.CallOption {
hdrs := make([]rpc.CallOption, 0, len(headers)+1)
hdrs = append(hdrs, rpc.WithHeader(_version, dosa.VERSION))
for h, v := range headers {
hdrs = append(hdrs, rpc.WithHeader(h, v))
}
return hdrs
}
13 changes: 13 additions & 0 deletions connectors/yarpc/helpers_test.go
Expand Up @@ -21,6 +21,7 @@
package yarpc

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -234,3 +235,15 @@ func TestEncodeOperator(t *testing.T) {
assert.Equal(t, test.rpcop, *encodeOperator(test.dop))
}
}

func TestGetHeaders(t *testing.T) {
headers := map[string]string{
"Foo": "bar",
"Bar": "foo",
}
hdrs := getHeaders(headers)
assert.Equal(t, len(headers)+1, len(hdrs))
for _, h := range hdrs {
assert.Equal(t, "yarpc.CallOption", reflect.TypeOf(h).String())
}
}

0 comments on commit 8ab04ef

Please sign in to comment.