-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Indexing permissions as part of the Elastic Agent policy * Delay the output key generation. Now it is dirven by the policy monitor policy updates. (cherry picked from commit a743bad) # Conflicts: # cmd/fleet/handleCheckin.go # cmd/fleet/handleEnroll.go Co-authored-by: Aleksandr Maus <[email protected]>
- Loading branch information
1 parent
23bb5b3
commit 2729c2e
Showing
11 changed files
with
485 additions
and
62 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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package policy | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"encoding/json" | ||
"errors" | ||
|
||
"github.com/elastic/fleet-server/v7/internal/pkg/smap" | ||
) | ||
|
||
const ( | ||
DefaultOutputName = "default" | ||
OutputPermissionsProperty = "output_permissions" | ||
) | ||
|
||
var ( | ||
ErrOutputPermissionsNotFound = errors.New("output_permissions not found") | ||
ErrDefaultOutputNotFound = errors.New("default output not found") | ||
ErrInvalidPermissionsFormat = errors.New("invalid permissions format") | ||
) | ||
|
||
func GetRoleDescriptors(outputPermissionsRaw []byte) (hash string, roles []byte, err error) { | ||
if len(outputPermissionsRaw) == 0 { | ||
return | ||
} | ||
|
||
output, err := getDefaultOutputMap(outputPermissionsRaw) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Calculating the hash of the original output map | ||
hash, err = output.Hash() | ||
if err != nil { | ||
return | ||
} | ||
|
||
roles, err = json.Marshal(output) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
func CheckOutputPermissionsChanged(hash string, outputPermissionsRaw []byte) (newHash string, roles []byte, changed bool, err error) { | ||
if len(outputPermissionsRaw) == 0 { | ||
return | ||
} | ||
|
||
// shotcuircut, hash and compare as is, if equals the json is serialized consistently from jsacascript and go | ||
newHash, err = getDefaultOutputHash(outputPermissionsRaw) | ||
if err != nil { | ||
return | ||
} | ||
if hash == newHash { | ||
return hash, nil, false, nil | ||
} | ||
|
||
newHash, roles, err = GetRoleDescriptors(outputPermissionsRaw) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return newHash, roles, (newHash != hash), nil | ||
} | ||
|
||
func getDefaultOutputHash(outputPermissionsRaw []byte) (hash string, err error) { | ||
var m map[string]json.RawMessage | ||
err = json.Unmarshal(outputPermissionsRaw, &m) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if len(m[DefaultOutputName]) == 0 { | ||
return | ||
} | ||
|
||
b := sha256.Sum256(m[DefaultOutputName]) | ||
return hex.EncodeToString(b[:]), nil | ||
} | ||
|
||
func getDefaultOutputMap(outputPermissionsRaw []byte) (defaultOutput smap.Map, err error) { | ||
outputPermissions, err := smap.Parse(outputPermissionsRaw) | ||
if err != nil { | ||
return | ||
} | ||
|
||
defaultOutput = outputPermissions.GetMap(DefaultOutputName) | ||
if defaultOutput == nil { | ||
err = ErrDefaultOutputNotFound | ||
} | ||
return | ||
} |
Oops, something went wrong.