mirror of
https://github.com/pikami/cosmium.git
synced 2025-12-20 17:30:30 +00:00
Fixed authentication key generation for partition key ranges
Fixed collection rid generation Improved compatibility with SDKs
This commit is contained in:
@@ -10,6 +10,11 @@ import (
|
||||
|
||||
// https://learn.microsoft.com/en-us/rest/api/cosmos-db/access-control-on-cosmosdb-resources
|
||||
func GenerateSignature(verb string, resourceType string, resourceId string, date string, masterKey string) string {
|
||||
isNameBased := resourceId != "" && ((len(resourceId) > 4 && resourceId[3] == '/') || strings.HasPrefix(strings.ToLower(resourceId), "interopusers"))
|
||||
if !isNameBased {
|
||||
resourceId = strings.ToLower(resourceId)
|
||||
}
|
||||
|
||||
payload := fmt.Sprintf(
|
||||
"%s\n%s\n%s\n%s\n%s\n",
|
||||
strings.ToLower(verb),
|
||||
|
||||
@@ -27,4 +27,9 @@ func Test_GenerateSignature(t *testing.T) {
|
||||
signature := authentication.GenerateSignature("DELETE", "dbs", "dbs/Test Database", testDate, config.DefaultAccountKey)
|
||||
assert.Equal(t, "LcuXXg0TcXxZG0kUCj9tZIWRy2yCzim3oiqGiHpRqGs=", signature)
|
||||
})
|
||||
|
||||
t.Run("Should generate PKRANGES signature", func(t *testing.T) {
|
||||
signature := authentication.GenerateSignature("GET", "pkranges", "m4d+xG08uVM=", testDate, config.DefaultAccountKey)
|
||||
assert.Equal(t, "6S5ceZsl2EXWB3Jo5bJcK7zv8NxXnsxWPWD9TH3nNMo=", signature)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -66,7 +66,8 @@ func CreateDocument(databaseId string, collectionId string, document map[string]
|
||||
var database repositorymodels.Database
|
||||
var collection repositorymodels.Collection
|
||||
if documentId, ok = document["id"].(string); !ok || documentId == "" {
|
||||
return repositorymodels.Document{}, repositorymodels.BadRequest
|
||||
documentId = fmt.Sprint(uuid.New())
|
||||
document["id"] = documentId
|
||||
}
|
||||
|
||||
if database, ok = storeState.Databases[databaseId]; !ok {
|
||||
|
||||
@@ -2,6 +2,7 @@ package resourceid
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"math/rand"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -10,6 +11,12 @@ func New() string {
|
||||
id := uuid.New().ID()
|
||||
idBytes := uintToBytes(id)
|
||||
|
||||
// first byte should be bigger than 0x80 for collection ids
|
||||
// clients classify this id as "user" otherwise
|
||||
if (idBytes[0] & 0x80) <= 0 {
|
||||
idBytes[0] = byte(rand.Intn(0x80) + 0x80)
|
||||
}
|
||||
|
||||
return base64.StdEncoding.EncodeToString(idBytes)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user