GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — sensor/settings_migration.go at main · openctemio/sensor

Expand file tree

/

settings_migration.go

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
78 lines (72 loc) · 2.75 KB
/

settings_migration.go

Copy path

File metadata and controls

78 lines (72 loc) · 2.75 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
// Settings from before the agent -> sensor rename (RFC-023 §9.5, "Upgrade
// migration for existing installations"). Installations set them in .env
// files, systemd units, compose files, Helm values and CI pipelines, so an
// upgraded sensor must keep working with them unchanged:
//
// - the new name is read first;
// - an old name is applied to the new one, with a startup warning naming
// both;
// - startup is refused only when an old and a new name are both set to
// different values (silently picking one could, for example, switch
// private-network scanning on or off).
//
// Environment variables are not migrated: the SDK refuses the removed names
// (AGENT_ID, AGENT_NAME, AGENT_ALLOW_PRIVATE_TARGETS, API_URL, API_KEY,
// REGION, PLATFORM_KEY_AUTORENEW, ...) at start-up, naming each replacement
// (sensorkit.RemovedEnv). It still migrates the -agent-id flag and moves the
// credentials file (~/.openctem/agent-credentials.json,
// platform.ResolveCredentialsFile); this file migrates the sensor's own
// configuration file.
import (
"fmt"
"reflect"
"github.com/openctemio/sdk-go/pkg/sensorproto/legacyv1"
"gopkg.in/yaml.v3"
)
// migrateConfigFile applies the pre-rename keys of a -config file: the
// top-level `agent:` block is now `sensor:` and `server.agent_id` is now
// `server.sensor_id`. data is the file after environment expansion; cfg has
// already been decoded from it with the new keys.
func migrateConfigFile(data []byte, cfg *Config) error {
var keys struct {
// yaml.v3 fills a yaml.Node value (not a pointer); Kind is zero when
// the key is absent.
Sensor yaml.Node `yaml:"sensor"`
LegacyBlock yaml.Node `yaml:"agent"`
Server struct {
SensorID *string `yaml:"sensor_id"`
LegacySensorID *string `yaml:"agent_id"`
} `yaml:"server"`
}
if err := yaml.Unmarshal(data, &keys); err != nil {
return fmt.Errorf("parse config: %w", err)
}
if keys.LegacyBlock.Kind != 0 {
var old SensorSettings
if err := keys.LegacyBlock.Decode(&old); err != nil {
return fmt.Errorf("parse config: agent: %w", err)
}
if keys.Sensor.Kind != 0 && !reflect.DeepEqual(old, cfg.Sensor) {
return &legacyv1.ConflictError{Old: "agent: block", New: "sensor: block"}
}
legacyv1.Deprecated("agent:", "sensor:", "configuration key")
cfg.Sensor = old
}
given := map[string]string{}
if keys.Server.SensorID != nil {
given["server.sensor_id"] = *keys.Server.SensorID
}
if keys.Server.LegacySensorID != nil {
given["server.agent_id"] = *keys.Server.LegacySensorID
}
id, ok, err := legacyv1.Resolve("server.sensor_id", "server.agent_id", "configuration key",
func(k string) (string, bool) { v, ok := given[k]; return v, ok })
if err != nil {
return err
}
if ok {
cfg.API.SensorID = id
}
return nil
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.