Skip to content

Commit

Permalink
fix: ATOM-13 network.tls context incorrectly generated (#47)
Browse files Browse the repository at this point in the history
* fix: ATOM-13 network.tls context incorrectly generated

* fix linter
  • Loading branch information
Jesse S authored Mar 19, 2024
1 parent c52ab05 commit 1600222
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 21 deletions.
43 changes: 33 additions & 10 deletions asconfig/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,11 +519,11 @@ func undefinedOrNull(val interface{}) bool {
return false
}

// convertIndexedToList converts an indexed key to a list key. It returns the
// convertIndexedToStringList converts an indexed key to a list key. It returns the
// new key and the value as a string. If the key is not indexed or the value is
// not a string, it returns empty strings.
func convertIndexedToList(key string, value interface{}) (newKey, strVal string) {
if newKey, _, _ = parseIndexField(key); newKey != "" {
func convertIndexedToStringList(key string, value interface{}) (newKey, strVal string) {
if newKey, _, _, _ = parseIndexField(key); newKey != "" {
if str, ok := value.(string); ok {
strVal = str
return newKey, strVal
Expand All @@ -533,20 +533,20 @@ func convertIndexedToList(key string, value interface{}) (newKey, strVal string)
return newKey, strVal
}

func parseIndexField(key string) (newKey string, index int, err error) {
func parseIndexField(key string) (newKey string, index int, extra string, err error) {
if match := indexedRe.FindStringSubmatch(key); match != nil {
index, err = strconv.Atoi(match[2])

if err != nil {
return "", 0, err
return "", 0, "", err
}

newKey = match[1]

return newKey, index, nil
return newKey, index, match[3], nil
}

return newKey, index, nil
return newKey, index, extra, nil
}

// execute transforms key values in the config.
Expand All @@ -557,6 +557,7 @@ func (s *transformKeyValuesStep) execute(conf Conf) error {
newFlatConf := make(Conf, len(origFlatConf)) // we will overwrite flat_config
sortedKeys := sortKeys(origFlatConf)
scNamespaces := []string{}
networkTLSNames := map[int]string{}

for _, key := range sortedKeys {
// We will mutate the servers key, value response to match the schema
Expand All @@ -578,11 +579,11 @@ func (s *transformKeyValuesStep) execute(conf Conf) error {
key = key + sep + keyType
}

if newKey, str := convertIndexedToList(key, value); newKey != "" {
if newKey, str := convertIndexedToStringList(key, value); newKey != "" {
newKey = getPluralKey(newKey)

if strings.HasSuffix(key, "shadow") {
_, index, err := parseIndexField(key)
if strings.HasSuffix(key, "shadow") { //nolint:gocritic // This can't be rewritten as a switch
_, index, _, err := parseIndexField(key)
if err != nil {
s.log.V(-1).Error(err, "Error parsing index field for shadow device")
return err
Expand All @@ -599,6 +600,28 @@ func (s *transformKeyValuesStep) execute(conf Conf) error {
sliceVal := newFlatConf[newKey].([]string)
sliceVal[index] = sliceVal[index] + " " + str
value = sliceVal
} else if newKey == "network.tls" {
_, index, field, err := parseIndexField(key)
if err != nil {
s.log.V(-1).Error(err, "Error parsing index field for network.tls")
return err
}

if _, ok := networkTLSNames[index]; !ok {
nameKey := fmt.Sprintf("network%stls[%d]%sname", sep, index, sep)

if val, ok := origFlatConf[nameKey].(string); ok {
newIndexKey := "network" + sep + "tls" + sep + "{" + val + "}" + sep + keyIndex

newFlatConf[newIndexKey] = index
networkTLSNames[index] = val
} else {
s.log.V(-1).Info("No name found for network.tls", "index", index)
return fmt.Errorf("no name found for network.tls[%d]", index)
}
}

newKey = "network" + sep + "tls" + sep + "{" + networkTLSNames[index] + "}" + field
} else {
if _, ok := newFlatConf[newKey]; ok {
if _, ok := newFlatConf[newKey].([]string); ok {
Expand Down
60 changes: 49 additions & 11 deletions asconfig/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -919,9 +919,27 @@ var networkTC = GenerateTC{
"service.port": 3000,
"service.tls-access-port": 0,
"service.tls-alternate-access-port": 0,
"service.tls-name": "null",
"service.tls-port": 0,
"service.tls-authenticate-client": 0,
"service.tls-authenticate-client": false,
"service.tls-name": "aerospike-a-2.test-runner",
"service.tls-port": 4333,
"tls[0].ca-file": "null",
"tls[0].ca-path": "null",
"tls[0].cert-blacklist": "null",
"tls[0].cert-file": "/data/certs/file0.pem",
"tls[0].cipher-suite": "null",
"tls[0].key-file": "/data/certs/key0.pem",
"tls[0].key-file-password": "null",
"tls[0].name": "aerospike-a-0",
"tls[0].protocols": "TLSv1.2",
"tls[1].ca-file": "null",
"tls[1].ca-path": "null",
"tls[1].cert-blacklist": "null",
"tls[1].cert-file": "/data/certs/file1.pem",
"tls[1].cipher-suite": "null",
"tls[1].key-file": "/data/certs/key1.pem",
"tls[1].key-file-password": "null",
"tls[1].name": "aerospike-a-1",
"tls[1].protocols": "TLSv1.2",
},
},
Conf{"metadata": Conf{"asd_build": "6.4.0.0", "node_id": "BB9030011AC4202"}},
Expand Down Expand Up @@ -971,9 +989,33 @@ var networkTC = GenerateTC{
"port": 3000,
"tls-access-port": 0,
"tls-alternate-access-port": 0,
"tls-name": "",
"tls-port": 0,
"tls-authenticate-client": 0,
"tls-name": "aerospike-a-2.test-runner",
"tls-port": 4333,
"tls-authenticate-client": false,
},
"tls": []Conf{
{
"ca-file": "",
"ca-path": "",
"cert-blacklist": "",
"cert-file": "/data/certs/file0.pem",
"cipher-suite": "",
"key-file": "/data/certs/key0.pem",
"key-file-password": "",
"name": "aerospike-a-0",
"protocols": "TLSv1.2",
},
{
"ca-file": "",
"ca-path": "",
"cert-blacklist": "",
"cert-file": "/data/certs/file1.pem",
"cipher-suite": "",
"key-file": "/data/certs/key1.pem",
"key-file-password": "",
"name": "aerospike-a-1",
"protocols": "TLSv1.2",
},
},
},
},
Expand Down Expand Up @@ -1801,7 +1843,6 @@ var namespacesDefaultsTC = GenerateTC{
"memory-size": 1073741824,
"rack-id": 2,
"replication-factor": 1,
"nsup-period": 0, // Should not be here. Default value for nsup schema is wrong.
"sets": []Conf{
{
"name": "testset",
Expand All @@ -1819,9 +1860,7 @@ var namespacesDefaultsTC = GenerateTC{
"/opt/aerospike/data/bar.dat /opt/aerospike/data/bar-shadow.dat",
"/opt/aerospike/data/foo.dat /opt/aerospike/data/foo-shadow.dat",
},
"filesize": 1073741824,
"data-in-memory": false,
"post-write-queue": 256,
"filesize": 1073741824,
},
"strong-consistency": true,
},
Expand All @@ -1833,7 +1872,6 @@ var namespacesDefaultsTC = GenerateTC{
},
"rack-id": 1,
"replication-factor": 1,
"nsup-period": 0, // Should not be here. Default value for nsup schema is wrong.
"sets": []Conf{
{
"name": "testset",
Expand Down

0 comments on commit 1600222

Please sign in to comment.