Move sensitivity classification to shared history
This commit is contained in:
@@ -3,6 +3,8 @@ package engine
|
||||
import (
|
||||
"slices"
|
||||
"sort"
|
||||
|
||||
"gitea.maximumdirect.net/eric/ConfigKit/internal/document"
|
||||
)
|
||||
|
||||
type historySelection struct {
|
||||
@@ -21,6 +23,7 @@ type historySelection struct {
|
||||
type historySensitivityCandidate struct {
|
||||
path Path
|
||||
sensitive bool
|
||||
classify bool
|
||||
dependencies []Path
|
||||
}
|
||||
|
||||
@@ -200,16 +203,123 @@ func (model *historyModel) sensitivityCandidates() []historySensitivityCandidate
|
||||
return nil
|
||||
}
|
||||
candidates := make([]historySensitivityCandidate, 0, len(model.observations))
|
||||
for _, observation := range model.observations {
|
||||
candidate := historySensitivityCandidate{path: observation.path.clone(), sensitive: observation.sensitive}
|
||||
if attribution, found := model.resolveAttribution(observation.attributionRef); found {
|
||||
candidate.dependencies = attribution.dependencies
|
||||
indices := make(map[string][]int)
|
||||
add := func(path Path, sensitive, classify bool) int {
|
||||
if !path.valid() {
|
||||
return -1
|
||||
}
|
||||
candidates = append(candidates, candidate)
|
||||
pointer := path.String()
|
||||
for _, index := range indices[pointer] {
|
||||
if sameConcreteHistoryPath(candidates[index].path, path) {
|
||||
candidates[index].sensitive = candidates[index].sensitive || sensitive
|
||||
candidates[index].classify = candidates[index].classify || classify
|
||||
return index
|
||||
}
|
||||
}
|
||||
index := len(candidates)
|
||||
candidates = append(candidates, historySensitivityCandidate{path: path.clone(), sensitive: sensitive, classify: classify})
|
||||
indices[pointer] = append(indices[pointer], index)
|
||||
return index
|
||||
}
|
||||
for _, observation := range model.observations {
|
||||
concreteCandidate := observation.role == historyRoleControl
|
||||
if observation.role == historyRoleContribution {
|
||||
if application, found := model.applicationPayload(observation.payload); found && len(application.sequences) != 0 {
|
||||
concreteCandidate = true
|
||||
}
|
||||
}
|
||||
index := add(observation.path, observation.sensitive, concreteCandidate)
|
||||
if parsed, err := ParsePath(observation.path.String()); err == nil {
|
||||
add(parsed, observation.sensitive, true)
|
||||
}
|
||||
if index < 0 {
|
||||
continue
|
||||
}
|
||||
if attribution, found := model.resolveAttribution(observation.attributionRef); found {
|
||||
for _, dependency := range attribution.dependencies {
|
||||
if dependency.valid() {
|
||||
appendDistinctHistoryPath(&candidates[index].dependencies, dependency)
|
||||
add(dependency, false, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_, inherited := model.sensitivityDependencies()
|
||||
for _, link := range inherited {
|
||||
add(link.target, false, true)
|
||||
add(link.dependency, false, true)
|
||||
}
|
||||
return candidates
|
||||
}
|
||||
|
||||
func appendDistinctHistoryPath(paths *[]Path, path Path) {
|
||||
for _, existing := range *paths {
|
||||
if sameConcreteHistoryPath(existing, path) {
|
||||
return
|
||||
}
|
||||
}
|
||||
*paths = append(*paths, path.clone())
|
||||
}
|
||||
|
||||
// sensitivityDependencies returns the original attribution edges and the
|
||||
// bidirectional links represented by inheritance payloads. It intentionally
|
||||
// does not derive edges from the public metadata projection, which may omit
|
||||
// evidence needed to keep historical values redacted.
|
||||
func (model *historyModel) sensitivityDependencies() (dependencies, inherited []secretDependency) {
|
||||
if model == nil {
|
||||
return nil, nil
|
||||
}
|
||||
for _, observation := range model.observations {
|
||||
if observation.phase == historyPhaseInheritance {
|
||||
continue
|
||||
}
|
||||
attribution, found := model.resolveAttribution(observation.attributionRef)
|
||||
if !found {
|
||||
continue
|
||||
}
|
||||
for _, dependency := range attribution.dependencies {
|
||||
if observation.reportedPath.valid() && dependency.valid() {
|
||||
dependencies = append(dependencies, secretDependency{
|
||||
target: observation.reportedPath.clone(), dependency: dependency.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, payload := range model.inheritancePayloads {
|
||||
for _, operation := range payload.operations {
|
||||
if operation.control != nil {
|
||||
control := operation.control.path
|
||||
for _, contributor := range operation.controlOrigin.contributors {
|
||||
if control.valid() && contributor.source.valid() {
|
||||
inherited = append(inherited, secretDependency{
|
||||
target: control.clone(), dependency: contributor.source.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
if operation.transfer == nil || operation.parent == nil {
|
||||
continue
|
||||
}
|
||||
transfer := operation.transfer
|
||||
walkDocument(operation.parent, transfer.to, func(destination Path, _ *document.Node) {
|
||||
relative, ok := relativeHistoryPath(transfer.to, destination)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
source := appendInheritancePath(transfer.from, relative)
|
||||
if _, found := operation.parentOrigins[source.String()]; !found {
|
||||
return
|
||||
}
|
||||
inherited = append(inherited, secretDependency{
|
||||
target: destination.clone(), dependency: source,
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
return dependencies, inherited
|
||||
}
|
||||
|
||||
func (model *historyModel) resolveAttribution(ref historyAttributionRef) (historyAttribution, bool) {
|
||||
if model == nil || ref < 0 || int(ref) >= len(model.attributions) {
|
||||
return historyAttribution{}, false
|
||||
|
||||
Reference in New Issue
Block a user