Skip to content

Commit

Permalink
t3c will not clear update flag in error condition (#7696)
Browse files Browse the repository at this point in the history
* added .lua to plugin and remap checks

* removed deprecated io/ioutil and added cache config failure message

* added support for config file audit failures and removed deprecated io/ioutil

* added change log entry
  • Loading branch information
jpappa200 authored Aug 9, 2023
1 parent 007c94a commit ac165a9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 21 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- [##7605](https://github.com/apache/trafficcontrol/pull/#7605) *Traffic Ops* Fixes `cachegroups_request_comments` v5 apis to respond with `RFC3339` date/time Format.
- [#7621](https://github.com/apache/trafficcontrol/pull/7621) *Traffic Ops* Use ID token for OAuth authentication, not Access Token
- [#7694](https://github.com/apache/trafficcontrol/pull/7694) *t3c*, *Traffic Control Health Client* Upgrade to ATS 9.2
- [#7966](https://github.com/apache/trafficcontrol/pull/7696) *t3c* will no longer clear update flag when config failure occurs and will also give a cache config error msg on exit.

### Fixed
- [#4393](https://github.com/apache/trafficcontrol/issues/4393) *Traffic Ops* Fixed the error code and alert structure when TO is queried for a delivery service with no ssl keys.
Expand Down
12 changes: 8 additions & 4 deletions cache-config/t3c-apply/t3c-apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package main
import (
"encoding/json"
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -75,6 +74,7 @@ const LockFilePath = "/var/run/t3c.lock"
const LockFileRetryInterval = time.Second
const LockFileRetryTimeout = time.Minute

const CacheConfigFailureExitMsg = `CACHE CONFIG FAILURE`
const FailureExitMsg = `CRITICAL FAILURE, ABORTING`
const PostConfigFailureExitMsg = `CRITICAL FAILURE AFTER SETTING CONFIG, ABORTING`
const SuccessExitMsg = `SUCCESS`
Expand Down Expand Up @@ -346,7 +346,11 @@ func Main() int {
}

metaData.Succeeded = true
return GitCommitAndExit(ExitCodeSuccess, SuccessExitMsg, cfg, metaData, oldMetaData)
if syncdsUpdate == torequest.UpdateTropsFailed {
return GitCommitAndExit(ExitCodeSuccess, CacheConfigFailureExitMsg, cfg, metaData, oldMetaData)
} else {
return GitCommitAndExit(ExitCodeSuccess, SuccessExitMsg, cfg, metaData, oldMetaData)
}
}

func LogPanic(f func() int) (exitCode int) {
Expand Down Expand Up @@ -450,7 +454,7 @@ func WriteMetaData(cfg config.Cfg, metaData *t3cutil.ApplyMetaData) {

metaDataFilePath := GetMetaDataFilePath(cfg)

if err := ioutil.WriteFile(metaDataFilePath, bts, MetaDataFileMode); err != nil {
if err := os.WriteFile(metaDataFilePath, bts, MetaDataFileMode); err != nil {
log.Errorln("writing metadata file '" + metaDataFilePath + "': " + err.Error())
return
}
Expand All @@ -459,7 +463,7 @@ func WriteMetaData(cfg config.Cfg, metaData *t3cutil.ApplyMetaData) {
func LoadMetaData(cfg config.Cfg) (*t3cutil.ApplyMetaData, error) {
metaDataFilePath := GetMetaDataFilePath(cfg)

bts, err := ioutil.ReadFile(metaDataFilePath)
bts, err := os.ReadFile(metaDataFilePath)
if err != nil {
return nil, errors.New("reading metadata file '" + metaDataFilePath + "': " + err.Error())
}
Expand Down
42 changes: 29 additions & 13 deletions cache-config/t3c-apply/torequest/torequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package torequest
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
Expand Down Expand Up @@ -102,6 +101,7 @@ type ConfigFile struct {
TropsBackup string // location to backup the TrafficOps Version
AuditComplete bool // audit is complete
AuditFailed bool // audit failed
AuditError string // Error generated when AuditFailed is true
ChangeApplied bool // a change has been applied
ChangeNeeded bool // change required
PreReqFailed bool // failed plugin prerequiste check
Expand Down Expand Up @@ -208,22 +208,33 @@ func (r *TrafficOpsReq) checkConfigFile(cfg *ConfigFile, filesAdding []string) e
}

if cfg.Dir == "" {
cfg.AuditFailed = true
return errors.New("No location information for " + cfg.Name)
}
// return if audit has already been done.
if cfg.AuditComplete == true {
if cfg.AuditComplete {
return nil
}

if !util.MkDirWithOwner(cfg.Dir, r.Cfg.ReportOnly, &cfg.Uid, &cfg.Gid) {
cfg.AuditFailed = true
return errors.New("Unable to create the directory '" + cfg.Dir + " for " + "'" + cfg.Name + "'")
}

log.Debugf("======== Start processing config file: %s ========\n", cfg.Name)

if cfg.Name == "50-ats.rules" {
err := r.processUdevRules(cfg)
if err != nil {
cfg.AuditFailed = true
return errors.New("unable to process udev rules in '" + cfg.Name + "': " + err.Error())
}
}

if cfg.Name == "remap.config" {
err := r.processRemapOverrides(cfg)
if err != nil {
cfg.AuditFailed = true
return err
}
}
Expand All @@ -232,6 +243,7 @@ func (r *TrafficOpsReq) checkConfigFile(cfg *ConfigFile, filesAdding []string) e
if cfg.Name == "remap.config" || cfg.Name == "plugin.config" {
if err := checkRefs(r.Cfg, cfg.Body, filesAdding); err != nil {
r.configFileWarnings[cfg.Name] = append(r.configFileWarnings[cfg.Name], "failed to verify '"+cfg.Name+"': "+err.Error())
cfg.AuditFailed = true
return errors.New("failed to verify '" + cfg.Name + "': " + err.Error())
}
log.Infoln("Successfully verified plugins used by '" + cfg.Name + "'")
Expand All @@ -251,18 +263,12 @@ func (r *TrafficOpsReq) checkConfigFile(cfg *ConfigFile, filesAdding []string) e
changeNeeded, err := diff(r.Cfg, cfg.Body, cfg.Path, r.Cfg.ReportOnly, cfg.Perm, cfg.Uid, cfg.Gid)

if err != nil {
cfg.AuditFailed = true
return errors.New("getting diff: " + err.Error())
}
cfg.ChangeNeeded = changeNeeded
cfg.AuditComplete = true

if cfg.Name == "50-ats.rules" {
err := r.processUdevRules(cfg)
if err != nil {
return errors.New("unable to process udev rules in '" + cfg.Name + "': " + err.Error())
}
}

log.Infof("======== End processing config file: %s for service: %s ========\n", cfg.Name, cfg.Service)
return nil
}
Expand Down Expand Up @@ -418,7 +424,7 @@ func (r *TrafficOpsReq) processUdevRules(cfg *ConfigFile) error {
}
}
}
fs, err := ioutil.ReadDir("/proc/fs/ext4")
fs, err := os.ReadDir("/proc/fs/ext4")
if err != nil {
log.Errorln("unable to read /proc/fs/ext4, cannot audit disks for filesystem usage.")
} else {
Expand Down Expand Up @@ -811,11 +817,12 @@ func (r *TrafficOpsReq) CheckReloadRestart(data []FileRestartData) RestartData {
// ProcessConfigFiles processes all config files retrieved from Traffic Ops.
func (r *TrafficOpsReq) ProcessConfigFiles(metaData *t3cutil.ApplyMetaData) (UpdateStatus, error) {
var updateStatus UpdateStatus = UpdateTropsNotNeeded
var auditErrors []string

log.Infoln(" ======== Start processing config files ========")

filesAdding := []string{} // list of file names being added, needed for verification.
for fileName, _ := range r.configFiles {
for fileName := range r.configFiles {
filesAdding = append(filesAdding, fileName)
}

Expand All @@ -841,6 +848,7 @@ func (r *TrafficOpsReq) ProcessConfigFiles(metaData *t3cutil.ApplyMetaData) (Upd
err := r.checkConfigFile(cfg, filesAdding)
if err != nil {
log.Errorln(err)
r.configFiles[cfg.Name].AuditError = err.Error()
}
}

Expand All @@ -857,11 +865,11 @@ func (r *TrafficOpsReq) ProcessConfigFiles(metaData *t3cutil.ApplyMetaData) (Upd
!cfg.AuditFailed {

changesRequired++
if cfg.Name == "plugin.config" && r.configFiles["remap.config"].PreReqFailed == true {
if cfg.Name == "plugin.config" && r.configFiles["remap.config"].PreReqFailed {
updateStatus = UpdateTropsFailed
log.Errorln("plugin.config changed however, prereqs failed for remap.config so I am skipping updates for plugin.config")
continue
} else if cfg.Name == "remap.config" && r.configFiles["plugin.config"].PreReqFailed == true {
} else if cfg.Name == "remap.config" && r.configFiles["plugin.config"].PreReqFailed {
updateStatus = UpdateTropsFailed
log.Errorln("remap.config changed however, prereqs failed for plugin.config so I am skipping updates for remap.config")
continue
Expand All @@ -876,9 +884,17 @@ func (r *TrafficOpsReq) ProcessConfigFiles(metaData *t3cutil.ApplyMetaData) (Upd
}
shouldRestartReload.ReloadRestart = append(shouldRestartReload.ReloadRestart, *reData)
}
} else if cfg.AuditFailed {
auditErrors = append(auditErrors, cfg.AuditError)
log.Warnf("audit failed for config file: %v Error: %s", cfg.Name, cfg.AuditError)
updateStatus = UpdateTropsFailed
}
}

if updateStatus == UpdateTropsFailed {
return UpdateTropsFailed, errors.New(strings.Join(auditErrors, "\n"))
}

r.RestartData = r.CheckReloadRestart(shouldRestartReload.ReloadRestart)

if 0 < len(r.changedFiles) {
Expand Down
8 changes: 4 additions & 4 deletions cache-config/t3c-check-refs/t3c-check-refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ func checkConfigLine(line string, lineNumber int, filesAdding map[string]struct{
}
}
} else if strings.HasPrefix(fields[ii], "@pparam") {
// any plugin parameters that end in '.config | .cfg | .txt | yml | .yaml'
// any plugin parameters that end in '.config | .cfg | .txt | yml | .yaml | .lua'
// are assumed to be configuration files and are checked that they
// exist in the filesystem at the absolute location in the name
// or relative to the ATS configuration files directory.
m := regexp.MustCompile(`^*(\.config|\.cfg|\.txt|\.yml|\.yaml)+`)
m := regexp.MustCompile(`^*(\.config|\.cfg|\.txt|\.yml|\.yaml|\.lua)+`)
sa := strings.Split(fields[ii], "=")
if len(sa) != 2 && len(sa) != 3 {
log.Errorf("malformed @pparam definition in remap.config on line '%d': %v\n", lineNumber, fields)
Expand Down Expand Up @@ -159,11 +159,11 @@ func checkConfigLine(line string, lineNumber int, filesAdding map[string]struct{
}
}
// Check the arguments in a plugin.config file for possible plugin config files.
// Any plugin argument that ends in '.config | .cfg | .txt | .yml | .yaml' are
// Any plugin argument that ends in '.config | .cfg | .txt | .yml | .yaml | .lua' are
// assumed to be configuration files and are checked that they
// exist in the filesystem at the absolute location in the name
// or relative to the ATS configuration files directory.
m := regexp.MustCompile(`([^=]+\.config$|[^=]\.cfg$|[^=]+\.txt$|[^=]+\.yml$|[^=]+\.yaml$)`)
m := regexp.MustCompile(`([^=]+\.config$|[^=]\.cfg$|[^=]+\.txt$|[^=]+\.yml$|[^=]+\.yaml$|[^=]+\.lua$)`)
for ii := 1; ii < length; ii++ {
param := strings.TrimSpace(fields[ii])
cfg := m.FindStringSubmatch(param)
Expand Down

0 comments on commit ac165a9

Please sign in to comment.