-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13162 from serathius/auth
etcdserver: Move read/update methods on Auth bucket to one place
- Loading branch information
Showing
3 changed files
with
173 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2021 The etcd 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 buckets | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"go.etcd.io/etcd/server/v3/mvcc/backend" | ||
) | ||
|
||
const ( | ||
revBytesLen = 8 | ||
) | ||
|
||
var ( | ||
authEnabled = []byte{1} | ||
authDisabled = []byte{0} | ||
) | ||
|
||
func UnsafeCreateAuthBucket(tx backend.BatchTx) { | ||
tx.UnsafeCreateBucket(Auth) | ||
} | ||
|
||
func UnsafeSaveAuthEnabled(tx backend.BatchTx, enabled bool) { | ||
if enabled { | ||
tx.UnsafePut(Auth, AuthEnabledKeyName, authEnabled) | ||
} else { | ||
tx.UnsafePut(Auth, AuthEnabledKeyName, authDisabled) | ||
} | ||
} | ||
|
||
func UnsafeReadAuthEnabled(tx backend.ReadTx) bool { | ||
_, vs := tx.UnsafeRange(Auth, AuthEnabledKeyName, nil, 0) | ||
if len(vs) == 1 { | ||
if bytes.Equal(vs[0], authEnabled) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func UnsafeSaveAuthRevision(tx backend.BatchTx, rev uint64) { | ||
revBytes := make([]byte, revBytesLen) | ||
binary.BigEndian.PutUint64(revBytes, rev) | ||
tx.UnsafePut(Auth, AuthRevisionKeyName, revBytes) | ||
} | ||
|
||
func UnsafeReadAuthRevision(tx backend.ReadTx) uint64 { | ||
_, vs := tx.UnsafeRange(Auth, AuthRevisionKeyName, nil, 0) | ||
if len(vs) != 1 { | ||
// this can happen in the initialization phase | ||
return 0 | ||
} | ||
return binary.BigEndian.Uint64(vs[0]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2021 The etcd 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 buckets | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.etcd.io/etcd/server/v3/mvcc/backend" | ||
betesting "go.etcd.io/etcd/server/v3/mvcc/backend/testing" | ||
) | ||
|
||
// TestAuthEnabled ensures that UnsafeSaveAuthEnabled&UnsafeReadAuthEnabled work well together. | ||
func TestAuthEnabled(t *testing.T) { | ||
tcs := []struct { | ||
enabled bool | ||
}{ | ||
{ | ||
enabled: true, | ||
}, | ||
{ | ||
enabled: false, | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(fmt.Sprint(tc.enabled), func(t *testing.T) { | ||
be, tmpPath := betesting.NewTmpBackend(t, time.Microsecond, 10) | ||
tx := be.BatchTx() | ||
if tx == nil { | ||
t.Fatal("batch tx is nil") | ||
} | ||
tx.Lock() | ||
UnsafeCreateAuthBucket(tx) | ||
UnsafeSaveAuthEnabled(tx, tc.enabled) | ||
tx.Unlock() | ||
be.ForceCommit() | ||
be.Close() | ||
|
||
b := backend.NewDefaultBackend(tmpPath) | ||
defer b.Close() | ||
v := UnsafeReadAuthEnabled(b.BatchTx()) | ||
|
||
assert.Equal(t, tc.enabled, v) | ||
}) | ||
} | ||
} | ||
|
||
// TestAuthRevision ensures that UnsafeSaveAuthRevision&UnsafeReadAuthRevision work well together. | ||
func TestAuthRevision(t *testing.T) { | ||
tcs := []struct { | ||
revision uint64 | ||
}{ | ||
{ | ||
revision: 0, | ||
}, | ||
{ | ||
revision: 1, | ||
}, | ||
{ | ||
revision: math.MaxUint64, | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(fmt.Sprint(tc.revision), func(t *testing.T) { | ||
be, tmpPath := betesting.NewTmpBackend(t, time.Microsecond, 10) | ||
tx := be.BatchTx() | ||
if tx == nil { | ||
t.Fatal("batch tx is nil") | ||
} | ||
tx.Lock() | ||
UnsafeCreateAuthBucket(tx) | ||
UnsafeSaveAuthRevision(tx, tc.revision) | ||
tx.Unlock() | ||
be.ForceCommit() | ||
be.Close() | ||
|
||
b := backend.NewDefaultBackend(tmpPath) | ||
defer b.Close() | ||
v := UnsafeReadAuthRevision(b.BatchTx()) | ||
|
||
assert.Equal(t, tc.revision, v) | ||
}) | ||
} | ||
} |