Skip to content

Commit a659d5f

Browse files
authored
feat: enable token hashing by default (#27388)
* feat: enable token hashing by default Enable token hashing by default. Pass "--use-token-hashing=false" on influxd command line to disable. - Make hashed tokens on by default for main influxd command. - Update command line help for "--use-hashed-tokens". - Remove "--use-hashed-tokens" command line parameter for `influxd upgrade` command. Tokens are now only converted when influxd is run with token hashing enabled. - Add log message when raw tokens are hashed at influxd startup. - Add log message indicating if hashed tokens are enabled. - Add log message when raw tokens are hashed at influxd startup. - Add log message indicating if hashed tokens are enabled. - Update tests as needed. - Update code comments to reflect token hashing is now default.
1 parent 95c7f17 commit a659d5f

6 files changed

Lines changed: 44 additions & 25 deletions

File tree

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ jobs:
811811
- attach_workspace:
812812
at: ~/project
813813
- run:
814-
command: ./bin/influxd_linux_amd64/influxd --store=memory --log-level=debug
814+
command: ./bin/influxd_linux_amd64/influxd --use-hashed-tokens=false --store=memory --log-level=debug
815815
background: true
816816
- run: mkdir -p ~/project/results
817817
- run:

authorization/storage.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ import (
2121
/*---
2222
Token storage and verification
2323
24-
Storage of hashed tokens has been added as an optional feature. This stores only the hash of a token
25-
in BoltDB. Token hashing is enabled with the `--use-hashed-tokens` option.
24+
Tokens are stored as hashes in BoltDB by default. Prior to version 2.8, only raw, unhashed tokens were
25+
supported. Opt-in support for hashed tokens was introduced in 2.8.0. Since 2.9.0, token hashing is on
26+
by default but still supports opt-out with "--use-hashed-tokens=false"
2627
2728
Upgrading the BoltDB schema is automatic on startup when using a new version of InfluxDB with token hashing support.
28-
Additionally, raw tokens are automatically migrated to hashed tokens if `--use-hashed-tokens` is configured.
29+
Additionally, raw tokens are automatically migrated to hashed tokens if whenever token hashing is enabled.
2930
Due to the schema changes, to use a version of InfluxDB without hashed token support, a manual downgrade using
3031
`influxd downgrade` must be run. Any tokens stored as hashed tokens will be unusable by the old version of InfluxDB
3132
and must be reset or recreated.
@@ -201,6 +202,8 @@ func NewStore(ctx context.Context, kvStore kv.Store, useHashedTokens bool, opts
201202
s.log = zap.NewNop()
202203
}
203204

205+
s.log.Info("Creating authorization store", zap.Bool("UseHashedTokens", useHashedTokens))
206+
204207
if err := s.setup(ctx); err != nil {
205208
return nil, fmt.Errorf("error during authorization store setup: %w", err)
206209
}
@@ -326,6 +329,10 @@ func (s *Store) MigrateTokens(ctx context.Context) error {
326329
return err
327330
}
328331

332+
if len(authsNeedingUpdate) > 0 {
333+
s.log.Info("Migrating raw tokens to hashed tokens", zap.Int("count", len(authsNeedingUpdate)))
334+
}
335+
329336
for batch := range slices.Chunk(authsNeedingUpdate, 100) {
330337
err := s.Update(ctx, func(tx kv.Tx) error {
331338
// Now update them. This really seems too simple, but s.UpdateAuthorization() is magical.

cmd/influxd/launcher/cmd.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ func NewOpts(viper *viper.Viper) *InfluxdOpts {
257257
HardeningEnabled: false,
258258
TemplateFileUrlsDisabled: false,
259259
StrongPasswords: false,
260+
UseHashedTokens: true,
260261
}
261262
}
262263

@@ -711,7 +712,7 @@ func (o *InfluxdOpts) BindCliOpts() []cli.Opt {
711712
DestP: &o.UseHashedTokens,
712713
Flag: "use-hashed-tokens",
713714
Default: o.UseHashedTokens,
714-
Desc: "enable storing hashed API tokens on disk (improves security, but prevents downgrades to < 2.8)",
715+
Desc: "enable storing hashed API tokens on disk (enabled by default; pass --use-hashed-tokens=false to disable for < 2.8 compatibility)",
715716
},
716717
}
717718
}

cmd/influxd/upgrade/upgrade.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,12 @@ func (o *optionsV1) populateDirs() {
8888
}
8989

9090
type optionsV2 struct {
91-
boltPath string
92-
cliConfigsPath string
93-
enginePath string
94-
cqPath string
95-
configPath string
96-
rmConflicts bool
97-
useHashedTokens bool
91+
boltPath string
92+
cliConfigsPath string
93+
enginePath string
94+
cqPath string
95+
configPath string
96+
rmConflicts bool
9897

9998
userName string
10099
password string
@@ -201,12 +200,6 @@ func NewCommand(ctx context.Context, v *viper.Viper) (*cobra.Command, error) {
201200
Default: filepath.Join(homeOrAnyDir(), "continuous_queries.txt"),
202201
Desc: "path for exported 1.x continuous queries",
203202
},
204-
{
205-
DestP: &options.target.useHashedTokens,
206-
Flag: "use-hashed-tokens",
207-
Default: options.target.useHashedTokens,
208-
Desc: "enable token hashing",
209-
},
210203
{
211204
DestP: &options.target.userName,
212205
Flag: "username",
@@ -661,7 +654,11 @@ func newInfluxDBv2(ctx context.Context, opts *optionsV2, log *zap.Logger) (svc *
661654
svc.ts.BucketService = storage.NewBucketService(log, svc.ts.BucketService, engine)
662655

663656
hashVariantName := authorization.DefaultHashVariantName // In the future this could come from opts.
664-
authStoreV2, err := authorization.NewStore(ctx, svc.store, opts.useHashedTokens, authorization.WithAuthorizationHashVariantName(hashVariantName), authorization.WithLogger(log))
657+
// We explicitly disable hashed tokens during upgrade processes. The actual token hashing
658+
// will be done on every influxd startup, so it doesn't have to be done now. Also, if the
659+
// user does not want to enable token hashing, having the upgrade command hash by default might
660+
// cause issues because they might not realize that the upgrade command also needs "--use-hashed-tokens=false".
661+
authStoreV2, err := authorization.NewStore(ctx, svc.store, false, authorization.WithAuthorizationHashVariantName(hashVariantName), authorization.WithLogger(log))
665662
if err != nil {
666663
return nil, err
667664
}

cmd/influxd/upgrade/upgrade_test.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import (
1313

1414
"github.com/BurntSushi/toml"
1515
"github.com/dustin/go-humanize"
16+
"github.com/go-crypt/crypt"
1617
"github.com/google/go-cmp/cmp"
1718
"github.com/influxdata/influx-cli/v2/clients"
1819
"github.com/influxdata/influxdb/v2"
1920
"github.com/influxdata/influxdb/v2/bolt"
2021
"github.com/influxdata/influxdb/v2/cmd/influxd/launcher"
2122
"github.com/influxdata/influxdb/v2/internal/testutil"
2223
"github.com/influxdata/influxdb/v2/kit/cli"
24+
"github.com/influxdata/influxdb/v2/pkg/crypt/algorithm/influxdb2"
2325
"github.com/influxdata/influxdb/v2/v1/services/meta"
2426
"github.com/spf13/cobra"
2527
"github.com/spf13/viper"
@@ -209,6 +211,7 @@ func TestUpgradeRealDB(t *testing.T) {
209211
configPath := filepath.Join(tl.Path, "config.toml")
210212

211213
v1opts := &optionsV1{configFile: v1ConfigPath}
214+
token := "my-token"
212215
v2opts := &optionsV2{
213216
boltPath: boltPath,
214217
enginePath: enginePath,
@@ -220,7 +223,7 @@ func TestUpgradeRealDB(t *testing.T) {
220223
orgName: "my-org",
221224
bucket: "my-bucket",
222225
retention: "7d",
223-
token: "my-token",
226+
token: token,
224227
}
225228

226229
opts := &options{source: *v1opts, target: *v2opts, force: true}
@@ -231,6 +234,7 @@ func TestUpgradeRealDB(t *testing.T) {
231234
v := viper.New()
232235
v.SetConfigFile(configPath)
233236
require.NoError(t, v.ReadInConfig())
237+
// Don't adjust lOpts.UseHashedTokens. This test will fail if UseHashedTokens does not default to true.
234238
lOpts := launcher.NewOpts(v)
235239
cliOpts := lOpts.BindCliOpts()
236240

@@ -315,17 +319,27 @@ func TestUpgradeRealDB(t *testing.T) {
315319
auths, _, err := tl.Launcher.AuthorizationService().FindAuthorizations(ctx, influxdb.AuthorizationFilter{})
316320
require.NoError(t, err)
317321
require.Len(t, auths, 1)
322+
require.NotEmpty(t, auths[0].HashedToken, "hashed token should not be empty when token hashing is enabled. Was UseHashedTokens default changed?")
323+
require.NotEqual(t, token, auths[0].HashedToken, "hashed token should not be the token.")
324+
require.Empty(t, auths[0].Token, "token should be empty when token hashing is enabled. Was UseHashedTokens default changed?")
325+
326+
// Verify that HashedToken has been hashed properly.
327+
decoder := crypt.NewDecoder()
328+
require.NoError(t, influxdb2.RegisterDecoder(decoder))
329+
digest, err := decoder.Decode(auths[0].HashedToken)
330+
require.NoError(t, err)
331+
require.True(t, digest.Match(token), "HashedToken does not match token")
318332

319-
respBody := mustRunQuery(t, tl, "test", "select count(avg) from stat", auths[0].Token)
333+
respBody := mustRunQuery(t, tl, "test", "select count(avg) from stat", token)
320334
require.Contains(t, respBody, `["1970-01-01T00:00:00Z",5776]`)
321335

322-
respBody = mustRunQuery(t, tl, "mydb", "select count(avg) from testv1", auths[0].Token)
336+
respBody = mustRunQuery(t, tl, "mydb", "select count(avg) from testv1", token)
323337
require.Contains(t, respBody, `["1970-01-01T00:00:00Z",2882]`)
324338

325-
respBody = mustRunQuery(t, tl, "mydb", "select count(i) from testv1", auths[0].Token)
339+
respBody = mustRunQuery(t, tl, "mydb", "select count(i) from testv1", token)
326340
require.Contains(t, respBody, `["1970-01-01T00:00:00Z",21]`)
327341

328-
respBody = mustRunQuery(t, tl, "mydb", `select count(line) from mydb."1week".log`, auths[0].Token)
342+
respBody = mustRunQuery(t, tl, "mydb", `select count(line) from mydb."1week".log`, token)
329343
require.Contains(t, respBody, `["1970-01-01T00:00:00Z",1]`)
330344

331345
cqBytes, err := os.ReadFile(cqPath)

scripts/ci/test-downgrade.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function wait_for_influxd () {
8787
function setup_influxd () {
8888
local -r influxd_path=$1 tmp=$2
8989
INFLUXD_BOLT_PATH="${tmp}/influxd.bolt" INFLUXD_SQLITE_PATH="${tmp}/influxd.sqlite" INFLUXD_ENGINE_PATH="${tmp}/engine" \
90-
"$influxd_path" &
90+
"$influxd_path" --use-hashed-tokens=false &
9191
local -r influxd_pid="$!"
9292

9393
wait_for_influxd "$influxd_pid"

0 commit comments

Comments
 (0)