Skip to content

Commit

Permalink
feat: support classic-flavored ingest keys (#237)
Browse files Browse the repository at this point in the history
## Which problem is this PR solving?

We've now released Ingest Keys, but in order for them to work with
Classic environments properly we need to update the key detection logic.

## Short description of the changes

- updates the Classic API Key detection logic to understand the shape of
a Classic Ingest Key
- makes the previously private `Config.isClassic` a public method
(`Config.IsClassic`) to allow other projects to use this method and
reduce the duplication of logic (i.e. beeline-go)
  • Loading branch information
jharley authored Feb 27, 2024
1 parent 4c81d25 commit 60fd25a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
17 changes: 14 additions & 3 deletions libhoney.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"os"
"path"
"reflect"
"regexp"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -73,6 +74,9 @@ var sd, _ = statsd.New(statsd.Mute(true), statsd.Prefix("libhoney"))
// expected format is product-name/version, eg "myapp/1.0"
var UserAgentAddition string

var classicKeyRegex = regexp.MustCompile(`^[a-f0-9]*$`)
var classicIngestKeyRegex = regexp.MustCompile(`^hc[a-z]ic_[a-z0-9]*$`)

// Config specifies settings for initializing the library.
type Config struct {

Expand Down Expand Up @@ -147,7 +151,7 @@ type Config struct {
}

func (c *Config) getDataset() string {
if c.isClassic() {
if c.IsClassic() {
if strings.TrimSpace(c.Dataset) == "" {
return defaultClassicDataset
}
Expand All @@ -164,8 +168,15 @@ func (c *Config) getDataset() string {
return trimmedDataset
}

func (c *Config) isClassic() bool {
return c.APIKey == "" || len(c.APIKey) == 32
func (c *Config) IsClassic() bool {
if len(c.APIKey) == 0 {
return true
} else if len(c.APIKey) == 32 {
return classicKeyRegex.MatchString(c.APIKey)
} else if len(c.APIKey) == 64 {
return classicIngestKeyRegex.MatchString(c.APIKey)
}
return false
}

// Init is called on app initialization and passed a Config struct, which
Expand Down
30 changes: 25 additions & 5 deletions libhoney_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1155,12 +1155,12 @@ func TestConfigVariationsForClassicNonClassic(t *testing.T) {
{
apikey: "",
dataset: "",
expectedDataset: "libhoney-go dataset",
expectedDataset: defaultClassicDataset,
},
{
apikey: "c1a551c000d68f9ed1e96432ac1a3380",
dataset: "",
expectedDataset: "libhoney-go dataset",
expectedDataset: defaultClassicDataset,
},
{
apikey: "c1a551c000d68f9ed1e96432ac1a3380",
Expand All @@ -1170,13 +1170,33 @@ func TestConfigVariationsForClassicNonClassic(t *testing.T) {
{
apikey: "d68f9ed1e96432ac1a3380",
dataset: "",
expectedDataset: "unknown_dataset",
expectedDataset: defaultDataset,
},
{
apikey: "d68f9ed1e96432ac1a3380",
dataset: " my-service ",
expectedDataset: "my-service",
},
{
apikey: "hcxik_1234567890123456789012345678901234567890123456789012345678",
dataset: "",
expectedDataset: defaultDataset,
},
{
apikey: "hcxik_1234567890123456789012345678901234567890123456789012345678",
dataset: "my-service",
expectedDataset: "my-service",
},
{
apikey: "hcxic_1234567890123456789012345678901234567890123456789012345678",
dataset: "",
expectedDataset: defaultClassicDataset,
},
{
apikey: "hcxic_1234567890123456789012345678901234567890123456789012345678",
dataset: "my-service",
expectedDataset: "my-service",
},
}

for _, tc := range tests {
Expand All @@ -1194,7 +1214,7 @@ func TestVerifyAPIKey(t *testing.T) {
APIKey string
expectedEnvironment string
}{
{Name: "classic", APIKey: "lcYrFflRUR6rHbIifwqhfGRUR6rHbIic", expectedEnvironment: ""},
{Name: "classic", APIKey: "f2b9746602fd36049b222d3e8c6c48c9", expectedEnvironment: ""},
{Name: "non-classic", APIKey: "lcYrFflRUR6rHbIifwqhfG", expectedEnvironment: "test_env"},
}

Expand All @@ -1209,7 +1229,7 @@ func TestVerifyAPIKey(t *testing.T) {
assert.Equal(t, "/1/auth", r.URL.Path)
assert.Equal(t, []string{tc.APIKey}, r.Header["X-Honeycomb-Team"])

if config.isClassic() {
if config.IsClassic() {
w.Write([]byte(`{"team":{"slug":"test_team"}}`))
} else {
w.Write([]byte(`{"team":{"slug":"test_team"},"environment":{"slug":"test_env"}}`))
Expand Down

0 comments on commit 60fd25a

Please sign in to comment.