Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

sentinel: carry the credential as Vector pastes it, quotes and all

shukieshah0e2f4fcd4601
diff --git a/cli/beacon/internal/endpoint/sentinel/forwarder.go b/cli/beacon/internal/endpoint/sentinel/forwarder.go
index ae5bef2e..f6c37104 100644
--- a/cli/beacon/internal/endpoint/sentinel/forwarder.go
+++ b/cli/beacon/internal/endpoint/sentinel/forwarder.go
@@ -196,6 +196,12 @@ func (o InstallOptions) settings() (Settings, error) {
if strings.TrimSpace(s.LogPath) == "" {
return s, errors.New("no runtime log path")
}
+ if err := validCredential("client secret", o.ClientSecret); err != nil {
+ return s, err
+ }
+ if err := validCredential("certificate password", o.CertificatePassword); err != nil {
+ return s, err
+ }
return s, nil
}
@@ -243,12 +249,12 @@ func renderForwarderConfig(o renderOptions) string {
fmt.Fprintf(&auth, "azure_tenant_id = %s\nazure_client_id = %s\n", q(s.TenantID), q(s.ClientID))
fmt.Fprintf(&auth, "certificate_path = %s\n", q(o.CertificatePath))
if o.HasCertificatePassword {
- fmt.Fprintf(&auth, "certificate_password = \"SECRET[%s.certificate_password]\"\n", secretBackend)
+ fmt.Fprintf(&auth, "certificate_password = %s\n", secretRef("certificate_password"))
}
default:
auth.WriteString("azure_credential_kind = \"client_secret_credential\"\n")
fmt.Fprintf(&auth, "azure_tenant_id = %s\nazure_client_id = %s\n", q(s.TenantID), q(s.ClientID))
- fmt.Fprintf(&auth, "azure_client_secret = \"SECRET[%s.client_secret]\"\n", secretBackend)
+ fmt.Fprintf(&auth, "azure_client_secret = %s\n", secretRef("client_secret"))
}
var secrets string
if s.Credential == CredentialClientSecret || o.HasCertificatePassword {
@@ -287,6 +293,22 @@ codec = "json"
}, s.LogPath)
}
+// secretRef is a reference Vector replaces with the secret's value. Vector pastes the value into the
+// config text as it is, before reading the TOML, so the reference sits in a multi-line literal
+// string, which takes quotes and backslashes as they are; validCredential refuses what it cannot.
+func secretRef(key string) string {
+ return fmt.Sprintf("'''SECRET[%s.%s]'''", secretBackend, key)
+}
+
+// validCredential refuses a credential the config cannot carry through Vector's secret backend: one
+// that would end the literal string, or a control character.
+func validCredential(name, value string) error {
+ if strings.Contains(value, "'''") || strings.IndexFunc(value, func(r rune) bool { return r < 0x20 || r == 0x7f }) >= 0 {
+ return fmt.Errorf("the %s contains three single quotes in a row or a control character, which Vector cannot take from its secret store", name)
+ }
+ return nil
+}
+
// manualVectorConfig is the pack's vector.toml for running Vector by hand: values come from the
// Vector service environment, and {{LOG_PATH}} is left for the pack installer.
func manualVectorConfig() string {
@@ -449,10 +471,9 @@ func Install(o InstallOptions) (InstallResult, error) {
// created in dir (readable only by root, or SYSTEM and Administrators) and removed straight after.
func validateConfig(vectorBin, dir, rendered string, secrets map[string]string) error {
for key, value := range secrets {
- // Doubled dollars, because Vector expands $NAME in the config text before it reads the
- // TOML, and the secret backend hands it the value untouched at runtime.
- literal := strings.ReplaceAll(siempack.TOMLEscape(value), "$", "$$")
- rendered = strings.ReplaceAll(rendered, fmt.Sprintf("SECRET[%s.%s]", secretBackend, key), literal)
+ // Exactly what Vector pastes in at runtime, inside the same literal string, with dollars
+ // doubled: here the value goes in before Vector expands $NAME in the text, at runtime after.
+ rendered = strings.ReplaceAll(rendered, fmt.Sprintf("SECRET[%s.%s]", secretBackend, key), strings.ReplaceAll(value, "$", "$$"))
}
f, err := os.CreateTemp(dir, ".validate-*.toml")
if err != nil {
diff --git a/cli/beacon/internal/endpoint/sentinel/forwarder_test.go b/cli/beacon/internal/endpoint/sentinel/forwarder_test.go
index 45c08b7e..ceea6bb0 100644
--- a/cli/beacon/internal/endpoint/sentinel/forwarder_test.go
+++ b/cli/beacon/internal/endpoint/sentinel/forwarder_test.go
@@ -52,7 +52,7 @@ func (f *fakeService) Unload() error {
f.running = false
return nil
}
-func (f *fakeService) RemoveUnits() { f.removed++ }
+func (f *fakeService) RemoveUnits() { f.removed++ }
func (f *fakeService) Status() service.Status {
return service.Status{Label: f.spec.Label, Loaded: f.running, Running: f.running}
}
@@ -115,7 +115,7 @@ func TestForwarderInstallWritesStateValidatesThenRunsTheService(t *testing.T) {
`endpoint = "https://beacon-dcr-abcd.eastus-1.ingest.monitor.azure.com"`,
`dcr_immutable_id = "dcr-0123456789abcdef0123456789abcdef"`,
`azure_tenant_id = "11111111-2222-3333-4444-555555555555"`,
- `azure_client_secret = "SECRET[beacon_sentinel.client_secret]"`,
+ "azure_client_secret = '''SECRET[beacon_sentinel.client_secret]'''",
`[secret.beacon_sentinel]`,
`read_from = "beginning"`,
`include = ["/var/log/beacon-agent/runtime.jsonl", "/var/log/beacon-agent/runtime.jsonl.1"]`,
@@ -237,7 +237,7 @@ func TestForwarderInstallWithACertificate(t *testing.T) {
t.Fatal(err)
}
config = readFile(t, filepath.Join(f.dir, configFileName))
- if !strings.Contains(config, `certificate_password = "SECRET[beacon_sentinel.certificate_password]"`) || strings.Contains(config, "pfx-password") {
+ if !strings.Contains(config, "certificate_password = '''SECRET[beacon_sentinel.certificate_password]'''") || strings.Contains(config, "pfx-password") {
t.Fatalf("certificate config with a password:\n%s", config)
}
if !strings.Contains(readFile(t, filepath.Join(f.dir, secretsFileName)), "pfx-password") {
@@ -449,15 +449,15 @@ func TestForwarderValidatesWithTheCredentialAndRunsWithTheReference(t *testing.T
t.Fatal(err)
}
o := validInstall()
- o.ClientSecret, o.CertificatePath, o.CertificatePassword = "", pfx, `pfx "pa$s"`
+ o.ClientSecret, o.CertificatePath, o.CertificatePassword = "", pfx, `p"a\$s'`
if _, err := Install(o); err != nil {
t.Fatal(err)
}
- if len(checked) != 1 || !strings.Contains(checked[0], `certificate_password = "pfx \"pa$$s\""`) {
+ if len(checked) != 1 || !strings.Contains(checked[0], `certificate_password = '''p"a\$$s''''`) {
t.Fatalf("the checked copy must carry the password itself:\n%v", checked)
}
config := readFile(t, filepath.Join(f.dir, configFileName))
- if strings.Contains(config, "pa$s") || !strings.Contains(config, "SECRET[beacon_sentinel.certificate_password]") {
+ if strings.Contains(config, `p"a`) || !strings.Contains(config, "SECRET[beacon_sentinel.certificate_password]") {
t.Fatalf("the installed config must hold only the reference:\n%s", config)
}
matches, _ := filepath.Glob(filepath.Join(f.dir, "*", ".validate-*"))
@@ -472,7 +472,7 @@ func TestForwarderValidatesWithTheCredentialAndRunsWithTheReference(t *testing.T
if _, err := Reconcile(ReconcileOptions{}); err != nil {
t.Fatal(err)
}
- if len(checked) != 2 || !strings.Contains(checked[1], `certificate_password = "pfx \"pa$$s\""`) {
+ if len(checked) != 2 || !strings.Contains(checked[1], `certificate_password = '''p"a\$$s''''`) {
t.Fatalf("reconcile must check a copy with the password in it")
}
}
@@ -552,3 +552,15 @@ func TestForwarderReconcileRecordsAMovedLog(t *testing.T) {
t.Fatalf("settings = %+v (%v)", settings, err)
}
}
+
+// A credential Vector's secret backend cannot carry into the config is refused before anything runs.
+func TestForwarderRefusesACredentialVectorCannotTake(t *testing.T) {
+ newForwarderFixture(t)
+ for _, secret := range []string{"ends'''here", "line\nbreak", "tab\tin"} {
+ o := validInstall()
+ o.ClientSecret = secret
+ if _, err := Install(o); err == nil || !strings.Contains(err.Error(), "secret store") {
+ t.Errorf("secret %q: %v", secret, err)
+ }
+ }
+}