/
validation.go
170 lines (146 loc) · 5.17 KB
/
validation.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
Copyright 2020 The Magma Authors.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// DB validation script to run as a sanity check after a completed migration.
// This loads all existing configs from old magmad and mesh service tables
// and verifies that they all exist in the new config service tables.
package migration
import (
"bytes"
"database/sql"
"fmt"
"github.com/go-magma/magma/orc8r/cloud/go/sqorc"
"github.com/go-magma/magma/orc8r/cloud/go/tools/migrations"
"github.com/golang/glog"
)
type TypeAndKey struct {
Type, Key string
}
func Validate(dbDriver string, dbSource string) error {
db, err := sqorc.Open(dbDriver, dbSource)
if err != nil {
return fmt.Errorf("Could not open DB connection: %s", err)
}
tx, err := db.Begin()
if err != nil {
return fmt.Errorf("Error opening transaction: %s", err)
}
_, err = tx.Exec("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE")
if err != nil {
return fmt.Errorf("Error setting transaction mode to serializable: %s", err)
}
allNetworkConfigs, err := getAllConfigs(tx, NetworkConfigTable)
if err != nil {
tx.Rollback()
return fmt.Errorf("Error loading networks: %s", err)
}
sortedNetworkIds := getSortedConfigIds(allNetworkConfigs)
for _, networkId := range sortedNetworkIds {
glog.V(2).Infof("Validating network %s", networkId)
if err := ValidateNetwork(tx, networkId, allNetworkConfigs[networkId]); err != nil {
tx.Rollback()
return err
}
}
err = tx.Commit()
if err != nil {
return fmt.Errorf("Error committing read-only transaction: %s", err)
}
return nil
}
func ValidateNetwork(tx *sql.Tx, networkId string, oldNetworkConfig *Config) error {
gatewayConfigTable := migrations.GetTableName(networkId, GatewayConfigTable)
meshConfigTable := migrations.GetTableName(networkId, MeshConfigTable)
oldGatewayConfigs, err := getAllConfigs(tx, gatewayConfigTable)
if err != nil {
return err
}
oldMeshConfigs, err := migrations.GetAllValuesFromTable(tx, meshConfigTable)
if err != nil {
return err
}
newConfigs, err := loadAllNewConfigs(tx, networkId)
if err != nil {
return err
}
if err := ValidateNetworkConfigs(networkId, oldNetworkConfig, newConfigs); err != nil {
return fmt.Errorf("Failed validation for network %s: %s", networkId, err)
}
if err := ValidateGatewayConfigs(oldGatewayConfigs, newConfigs); err != nil {
return fmt.Errorf("Failed validation for gateways for network %s: %s", networkId, err)
}
for meshId, oldVal := range oldMeshConfigs {
if err := ValidateMeshConfigs(meshId, oldVal, newConfigs); err != nil {
return fmt.Errorf("Failed vaidation for mesh %s on network %s: %s", networkId, meshId, err)
}
}
return nil
}
func ValidateNetworkConfigs(networkId string, oldNetworkConfig *Config, newConfigs map[TypeAndKey][]byte) error {
return validateMagmadConfigs(networkId, oldNetworkConfig, newConfigs, newNetworkTypesByOldKey)
}
func ValidateGatewayConfigs(oldGatewayConfigs map[string]*Config, newConfigs map[TypeAndKey][]byte) error {
for gatewayId, gatewayConfig := range oldGatewayConfigs {
if err := validateMagmadConfigs(gatewayId, gatewayConfig, newConfigs, newGatewayTypesByOldKey); err != nil {
return err
}
}
return nil
}
func ValidateMeshConfigs(meshId string, oldMeshConfig []byte, newConfigs map[TypeAndKey][]byte) error {
newVal, exists := newConfigs[TypeAndKey{Type: MeshType, Key: meshId}]
if !exists {
return fmt.Errorf("No corresponding new config for mesh %s", meshId)
}
if !bytes.Equal(oldMeshConfig, newVal) {
return fmt.Errorf("New config for mesh %s not equal to old", meshId)
}
return nil
}
func validateMagmadConfigs(id string, oldConfig *Config, newConfigs map[TypeAndKey][]byte, newTypesByOldKey map[string]string) error {
for oldKey, oldVal := range oldConfig.ConfigsByKey {
newType, ok := newTypesByOldKey[oldKey]
if !ok {
return fmt.Errorf("Unrecognized old config key %s for ID %s", oldKey, id)
}
// Skip empty config values
newVal, exists := newConfigs[TypeAndKey{Type: newType, Key: id}]
if newVal == nil || len(newVal) == 0 {
continue
}
if !exists {
return fmt.Errorf("Old config key %s not migrated for id %s", oldKey, id)
}
if !bytes.Equal(oldVal, newVal) {
return fmt.Errorf("Old config value for key %s not equal to new for id %s", oldKey, id)
}
}
return nil
}
func loadAllNewConfigs(tx *sql.Tx, networkId string) (map[TypeAndKey][]byte, error) {
table := migrations.GetTableName(networkId, NewConfigTable)
query := fmt.Sprintf("SELECT type, key, value FROM %s", table)
rows, err := tx.Query(query)
if err != nil {
return nil, err
}
defer rows.Close()
ret := map[TypeAndKey][]byte{}
for rows.Next() {
var configType, key string
var value []byte
err = rows.Scan(&configType, &key, &value)
if err != nil {
return nil, err
}
ret[TypeAndKey{Type: configType, Key: key}] = value
}
return ret, nil
}