Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Metadata tests followup #54

Merged
merged 7 commits into from
Aug 18, 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
24 changes: 17 additions & 7 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"crypto/hmac"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/binary"
"encoding/hex"
"encoding/json"
Expand Down Expand Up @@ -514,7 +515,7 @@ func (role *DelegatedRole) IsDelegatedPath(targetFilepath string) (bool, error)
// hash bin delegations - calculate the hash of the filepath to determine in which bin to find the target.
targetFilepathHash := sha256.Sum256([]byte(targetFilepath))
for _, pathHashPrefix := range role.PathHashPrefixes {
if strings.HasPrefix(string(targetFilepathHash[:]), pathHashPrefix) {
if strings.HasPrefix(base64.URLEncoding.EncodeToString(targetFilepathHash[:]), pathHashPrefix) {
return true, nil
}
}
Expand Down Expand Up @@ -573,8 +574,7 @@ func (role *SuccinctRoles) GetRolesForTarget(targetFilepath string) map[string]b
// GetRoles returns the names of all different delegated roles
func (role *SuccinctRoles) GetRoles() []string {
res := []string{}
numberOfBins := int(math.Pow(2, float64(role.BitLength)))
suffixLen := len(strconv.FormatInt(int64(numberOfBins-1), 16))
suffixLen, numberOfBins := role.GetSuffixLen()

for binNumber := 0; binNumber < numberOfBins; binNumber++ {
suffix := fmt.Sprintf("%0*x", suffixLen, binNumber)
Expand All @@ -583,11 +583,15 @@ func (role *SuccinctRoles) GetRoles() []string {
return res
}

func (role *SuccinctRoles) GetSuffixLen() (int, int) {
numberOfBins := int(math.Pow(2, float64(role.BitLength)))
return len(strconv.FormatInt(int64(numberOfBins-1), 16)), numberOfBins
}

// IsDelegatedRole returns whether the given roleName is in one of
// the delegated roles that “SuccinctRoles“ represents
func (role *SuccinctRoles) IsDelegatedRole(roleName string) bool {
numberOfBins := int64(math.Pow(2, float64(role.BitLength)))
suffixLen := len(strconv.FormatInt(int64(numberOfBins-1), 16))
suffixLen, numberOfBins := role.GetSuffixLen()

expectedPrefix := fmt.Sprintf("%s-", role.NamePrefix)

Expand All @@ -609,7 +613,7 @@ func (role *SuccinctRoles) IsDelegatedRole(roleName string) bool {
}

// check if the bin we calculated is indeed within the range of what we support
return (value >= 0) && (value < numberOfBins)
return (value >= 0) && (value < int64(numberOfBins))
}

// AddKey adds new signing key for delegated role "role"
Expand Down Expand Up @@ -674,9 +678,11 @@ func (signed *TargetsType) AddKey(key *Key, role string) error {
// standard delegated roles
if signed.Delegations.Roles != nil {
// loop through all delegated roles
isDelegatedRole := false
rdimitrov marked this conversation as resolved.
Show resolved Hide resolved
for i, d := range signed.Delegations.Roles {
// if role is found
if d.Name == role {
isDelegatedRole = true
// add key if keyID is not already part of keyIDs for that role
if !slices.Contains(d.KeyIDs, key.ID()) {
signed.Delegations.Roles[i].KeyIDs = append(signed.Delegations.Roles[i].KeyIDs, key.ID())
Expand All @@ -686,6 +692,9 @@ func (signed *TargetsType) AddKey(key *Key, role string) error {
log.Debugf("Delegated role %s already has keyID %s", role, key.ID())
}
}
if !isDelegatedRole {
return ErrValue{Msg: fmt.Sprintf("delegated role %s doesn't exist", role)}
}
} else if signed.Delegations.SuccinctRoles != nil {
// add key if keyID is not already part of keyIDs for the SuccinctRoles role
if !slices.Contains(signed.Delegations.SuccinctRoles.KeyIDs, key.ID()) {
Expand All @@ -696,7 +705,8 @@ func (signed *TargetsType) AddKey(key *Key, role string) error {
log.Debugf("SuccinctRoles role already has keyID %s", key.ID())

}
return ErrValue{Msg: fmt.Sprintf("delegated role %s doesn't exist", role)}
signed.Delegations.Keys[key.ID()] = key // TODO: should we check if we don't accidentally override an existing keyID with another key value?
return nil
}

// RevokeKey revokes key from delegated role "role" and updates the delegations key store
Expand Down
Loading
Loading