package hostops import ( "crypto/sha256" "encoding/hex" "fmt" "strings" "github.com/carlosframework/platform/internal/geoip" ) // classifyGeoip reviews the whole control/geoip/ family, exactly, as operator // platform state that holds no app record: the content-addressed database // blobs, the pointer to the live one, and the feed's status file. Nothing // else under the prefix is classified, and a body that is not what the // publisher writes at that key is refused, so a stray write cannot ride the // family. Deletes carry no body and are checked by key alone. func classifyGeoip(t MutationTarget) (Classification, error) { deleting := t.Method != "Put" switch { case t.Key == geoip.PointerKey: if !deleting { if _, err := geoip.ParsePointer(t.After); err != nil { return Classification{}, fmt.Errorf("%w: %s: %v", ErrUnclassified, t.Key, err) } } return Classification{Family: "geoip-pointer", NonApp: "operator GeoIP pointer; names one platform database blob and holds no app record"}, nil case t.Key == geoip.FeedKey: if !deleting { if _, err := geoip.ParseFeedStatus(t.After); err != nil { return Classification{}, fmt.Errorf("%w: %s: %v", ErrUnclassified, t.Key, err) } } return Classification{Family: "geoip-feed", NonApp: "operator GeoIP feed status; holds no app record"}, nil case IsGeoipBlobKey(t.Key): // A streamed put has no body here (PutFrom): the publisher hashed it // while staging and a create-only put cannot replace an object. A // buffered one must hash to its own name. if !deleting && len(t.After) != 0 { sum := sha256.Sum256(t.After) if t.Key != geoip.BlobKey(hex.EncodeToString(sum[:])) { return Classification{}, fmt.Errorf("%w: %s: body does not hash to its name", ErrUnclassified, t.Key) } } return Classification{Family: "geoip-blob", NonApp: "operator GeoIP database blob, content-addressed by its sha256; holds no app record"}, nil } return Classification{}, fmt.Errorf("%w: %s", ErrUnclassified, t.Key) } // IsGeoipBlobKey is exactly control/geoip/<64 lower-case hex>.mmdb. func IsGeoipBlobKey(key string) bool { rest, ok := strings.CutPrefix(key, geoip.Prefix) if !ok { return false } sha, ok := strings.CutSuffix(rest, ".mmdb") return ok && geoip.IsSHA256Hex(sha) }