Skip to content

Commit

Permalink
Issue open-horizon#4111 - Bug: secret_exists column not added to secr…
Browse files Browse the repository at this point in the history
…ets_policy if table already exists

Signed-off-by: Max McAdam <max@fredcom.com>
  • Loading branch information
MaxMcAdam committed Jul 24, 2024
1 parent 5ed9120 commit 6e7b059
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 34 deletions.
4 changes: 3 additions & 1 deletion agreementbot/persistence/postgresql/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ func (db *AgbotPostgresqlDB) Initialize(cfg *config.HorizonConfig) error {

// Run each SQL statement in the array of SQL statements for the current version.
for si := 0; si < len(migrationSQL[v].sql); si++ {
if _, err := db.db.Exec(migrationSQL[v].sql[si]); err != nil {
if _, err := db.db.Exec(migrationSQL[v].sql[si]); err != nil && migrationSQL[v].exitOnFailure {
return errors.New(fmt.Sprintf("unable to run SQL migration statement version %v, index %v, statement %v, error: %v", v, si, migrationSQL[v].sql[si], err))
} else if err != nil {
fmt.Printf("unable to run SQL migration statement version %v, index %v, statement %v, error: %v", v, si, migrationSQL[v].sql[si], err)
} else if _, err := db.db.Exec(VERSION_UPDATE, HIGHEST_DATABASE_VERSION, migrationSQL[v].description); err != nil {
return errors.New(fmt.Sprintf("unable to create version table, error: %v", err))
} else {
Expand Down
14 changes: 9 additions & 5 deletions agreementbot/persistence/postgresql/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@ const VERSION_QUERY = `SELECT ver, description, updated FROM version WHERE id =
const VERSION_INSERT = `DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM version WHERE id = 1) THEN
INSERT INTO version (id, ver, description) VALUES (1, 0, 'initial tables');
INSERT INTO version (id, ver, description) VALUES (1, 1, 'initial tables');
END IF;
END $$`

const VERSION_UPDATE = `UPDATE version SET ver = $1, description = $2, updated = current_timestamp WHERE id = 1;`

const HIGHEST_DATABASE_VERSION = v1
const HIGHEST_DATABASE_VERSION = v2
const v2 = 1
const v1 = 0

type SchemaUpdate struct {
sql []string // The SQL statements to run for an update to the schema.
description string // A description of the schema change.
sql []string // The SQL statements to run for an update to the schema.
description string // A description of the schema change.
exitOnFailure bool // true to quit if the update fails
}

var migrationSQL = map[int]SchemaUpdate{}
var v2SchemaUpdate = SchemaUpdate{sql: []string{"ALTER TABLE secrets_policy ADD COLUMN \"secret_exists\" BOOLEAN NOT NULL DEFAULT true;", "ALTER TABLE secrets_pattern ADD COLUMN \"secret_exists\" BOOLEAN NOT NULL DEFAULT true;"}, description: "Add a column to the secrets table to indicate if the secret exists or not. This is necessary to support node-specific secrets.", exitOnFailure: false}

var migrationSQL = map[int]SchemaUpdate{v2: v2SchemaUpdate}
38 changes: 19 additions & 19 deletions cli/cliutils/cliutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1098,29 +1098,29 @@ func GetIcpCertPath() string {

// TrustIcpCert adds the icp cert file to be trusted in calls made by the given http client
func TrustIcpCert(httpClient *http.Client) error {
icpCertPath := GetIcpCertPath()
icpCertPath := GetIcpCertPath()

var caCertPool *x509.CertPool
var err error
var caCertPool *x509.CertPool
var err error

// Trust the system certs like the anax agent code can
caCertPool, err = x509.SystemCertPool()
if err != nil {
// Trust the system certs like the anax agent code can
caCertPool, err = x509.SystemCertPool()
if err != nil {
// Decided not to fail and return here but just create a new pool
caCertPool = x509.NewCertPool()
}

if icpCertPath != "" {
icpCert, err := ioutil.ReadFile(icpCertPath)
if err != nil {
return fmt.Errorf(i18n.GetMessagePrinter().Sprintf("Encountered error reading ICP cert file %v: %v", icpCertPath, err))
}
caCertPool.AppendCertsFromPEM(icpCert)
}

transport := httpClient.Transport.(*http.Transport)
transport.TLSClientConfig.RootCAs = caCertPool
return nil
}

if icpCertPath != "" {
icpCert, err := ioutil.ReadFile(icpCertPath)
if err != nil {
return fmt.Errorf(i18n.GetMessagePrinter().Sprintf("Encountered error reading ICP cert file %v: %v", icpCertPath, err))
}
caCertPool.AppendCertsFromPEM(icpCert)
}

transport := httpClient.Transport.(*http.Transport)
transport.TLSClientConfig.RootCAs = caCertPool
return nil
}

// Get exchange url from /etc/default/horizon file. if not set, check /etc/horizon/anax.json file
Expand Down
14 changes: 7 additions & 7 deletions cli/eventlog/eventlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func Delete(selections []string, force bool) {
retCode, count := cliutils.HorizonDelete(url_s, []int{204}, []int{200}, false)

if retCode == http.StatusOK {
fmt.Println(i18n.GetMessagePrinter().Sprintf("Successfully deleted %v matching event log entries.", cliutils.RemoveQuotes(fmt.Sprintf("%v",count))))
fmt.Println(i18n.GetMessagePrinter().Sprintf("Successfully deleted %v matching event log entries.", cliutils.RemoveQuotes(fmt.Sprintf("%v", count))))
} else {
fmt.Println(i18n.GetMessagePrinter().Sprintf("No event log entries matching the given selectors were found."))
}
Expand All @@ -97,13 +97,13 @@ func Prune(force bool) {
cliutils.ConfirmRemove(i18n.GetMessagePrinter().Sprintf("Are you sure you want to remove all event logs from previous registrations?"))
}

retCode, count := cliutils.HorizonDelete(url, []int{204}, []int{200}, false)
retCode, count := cliutils.HorizonDelete(url, []int{204}, []int{200}, false)

if retCode == http.StatusOK {
fmt.Println(i18n.GetMessagePrinter().Sprintf("Successfully pruned %v matching event log entries.", cliutils.RemoveQuotes(fmt.Sprintf("%v",count))))
} else {
fmt.Println(i18n.GetMessagePrinter().Sprintf("No event log entries from previous registrations were found."))
}
if retCode == http.StatusOK {
fmt.Println(i18n.GetMessagePrinter().Sprintf("Successfully pruned %v matching event log entries.", cliutils.RemoveQuotes(fmt.Sprintf("%v", count))))
} else {
fmt.Println(i18n.GetMessagePrinter().Sprintf("No event log entries from previous registrations were found."))
}
}

func List(all bool, detail bool, selections []string, tailing bool) {
Expand Down
4 changes: 2 additions & 2 deletions persistence/eventlogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (w EventLogBase) Matches(selectors map[string][]Selector) bool {
case "timestamp":
attr = w.Timestamp
case "time_since":
attr = (uint64(time.Now().Unix()) - w.Timestamp)/3600
attr = (uint64(time.Now().Unix()) - w.Timestamp) / 3600
default:
return false // not tolerate wrong attribute name in the selector
}
Expand Down Expand Up @@ -436,7 +436,7 @@ func DeleteEventLogsWithSelectors(db *bolt.DB, selectors map[string][]Selector,
glog.Errorf("Unable to convert event source: %v. Error: %v", el.Source, err)
} else if (*esrc).Matches(source_selectors) {
b.Delete(k)
count ++
count++
}
}
}
Expand Down

0 comments on commit 6e7b059

Please sign in to comment.