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

Validate strings in user agent package #291

Merged
merged 1 commit into from
Feb 2, 2023
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
30 changes: 30 additions & 0 deletions useragent/patterns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package useragent

import (
"fmt"
"regexp"
)

var regexpSemVer = regexp.MustCompile(`^\d+\.\d+\.\d+$`)
var regexpAlphanum = regexp.MustCompile(`^[0-9A-Za-z_-]+$`)

func matchSemVer(s string) error {
if regexpSemVer.MatchString(s) {
return nil
}
return fmt.Errorf("invalid semver string: %s", s)
}

func matchAlphanum(s string) error {
if regexpAlphanum.MatchString(s) {
return nil
}
return fmt.Errorf("invalid alphanumeric string: %s", s)
}

func matchAlphanumOrSemVer(s string) error {
if regexpAlphanum.MatchString(s) || regexpSemVer.MatchString(s) {
return nil
}
return fmt.Errorf("invalid alphanumeric or semver string: %s", s)
}
31 changes: 31 additions & 0 deletions useragent/patterns_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package useragent

import (
"testing"

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

func TestMatchSemVer(t *testing.T) {
assert.NoError(t, matchSemVer("1.2.3"))
assert.Error(t, matchSemVer("1.2.3.4"))
assert.Error(t, matchSemVer("1.2.3-foo"))
assert.Error(t, matchSemVer("1.2"))
}

func TestMatchAlphanum(t *testing.T) {
assert.NoError(t, matchAlphanum("foo"))
assert.NoError(t, matchAlphanum("FOO"))
assert.NoError(t, matchAlphanum("FOO123"))
assert.NoError(t, matchAlphanum("foo_bar"))
assert.NoError(t, matchAlphanum("foo-bar"))
assert.Error(t, matchAlphanum("foo bar"))
assert.Error(t, matchAlphanum("foo/bar"))
}

func TestMatchAlphanumOrSemVer(t *testing.T) {
assert.NoError(t, matchAlphanumOrSemVer("foo"))
assert.NoError(t, matchAlphanumOrSemVer("1.2.3"))
assert.Error(t, matchAlphanumOrSemVer("foo/bar"))
assert.Error(t, matchAlphanumOrSemVer("1/2/3"))
}
45 changes: 29 additions & 16 deletions useragent/user_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@ import (
"golang.org/x/mod/semver"
)

const SdkName = "databricks-sdk-go"

// WithProduct is expected to be set by developers to differentiate
// their app from others. usage of `databricks.WithProduc` is preferred.
// WithProduct sets the product name and product version globally.
// It should be called by developers to differentiate their application from others.
func WithProduct(name, version string) {
// TODO: validate fields
product = name
if err := matchAlphanum(name); err != nil {
panic(err)
}
if err := matchSemVer(version); err != nil {
panic(err)
}
productName = name
productVersion = version
}

// product holds product name
var product = "unknown"

// productVersion holds different release version
var productName = "unknown"
var productVersion = "0.0.0"

const sdkName = "databricks-sdk-go"
const sdkVersion = version.Version

// extra holds per-process static extra information
// bits, that are agreed upfront with Databricks.
var extra data
Expand All @@ -39,25 +42,35 @@ const ctxAgent key = 5
// WithUserAgentExtra sets per-process extra user agent data,
// which integration developers have agreed with Databricks.
func WithUserAgentExtra(key, value string) {
// TODO: validate fields
if err := matchAlphanum(key); err != nil {
panic(err)
}
if err := matchAlphanumOrSemVer(value); err != nil {
panic(err)
}
extra = append(extra, info{key, value})
}

// InContext populates context with user agent dimension,
// usually to differentiate subsets of functionality and
// agreed with Databricks.
func InContext(ctx context.Context, k, v string) context.Context {
func InContext(ctx context.Context, key, value string) context.Context {
if err := matchAlphanum(key); err != nil {
panic(err)
}
if err := matchAlphanumOrSemVer(value); err != nil {
panic(err)
}
uac, _ := ctx.Value(ctxAgent).(data)
// TODO: validate fields
uac = append(uac, info{k, v})
uac = append(uac, info{key, value})
return context.WithValue(ctx, ctxAgent, uac)
}

// FromContext gets compliant user-agent string in a given context
func FromContext(ctx context.Context) string {
base := data{
{product, productVersion},
{SdkName, version.Version},
{productName, productVersion},
{sdkName, sdkVersion},
{"go", goVersion()},
{"os", runtime.GOOS},
}
Expand Down
4 changes: 4 additions & 0 deletions useragent/user_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ func TestFromContext_Custom(t *testing.T) {
expected := fmt.Sprintf(expectedFormat, version.Version, goVersion(), runtime.GOOS)
assert.Equal(t, expected, userAgent)
}

func TestDefaultsAreValid(t *testing.T) {
WithProduct(productName, productVersion)
}