|
| 1 | +package secureagent |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | +) |
| 9 | + |
| 10 | +func (a *Agent) copyConfigurationFile() error { |
| 11 | + log.Println("[INFO] Starting the Copy Configuration.") |
| 12 | + _ = a.doReportProgress(ProgressTypeConfigInitiated, "Configuration Initiated") |
| 13 | + // Copy the configuration file to the device |
| 14 | + file, err := os.Create(ARTIFACTS_PATH + a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference + "-config") |
| 15 | + if err != nil { |
| 16 | + log.Println("[ERROR] creating the configuration file", err.Error()) |
| 17 | + return err |
| 18 | + } |
| 19 | + defer func() { |
| 20 | + if err := file.Close(); err != nil { |
| 21 | + log.Println("[ERROR] Error when closing:", err) |
| 22 | + } |
| 23 | + }() |
| 24 | + |
| 25 | + plainTest, _ := base64.StdEncoding.DecodeString(a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.Configuration) |
| 26 | + _, err = file.WriteString(string(plainTest)) |
| 27 | + if err != nil { |
| 28 | + log.Println("[ERROR] writing the configuration file", err.Error()) |
| 29 | + return err |
| 30 | + } |
| 31 | + // nolint:gosec |
| 32 | + err = os.Chmod(ARTIFACTS_PATH+a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference+"-config", 0744) |
| 33 | + if err != nil { |
| 34 | + log.Println("[ERROR] changing the configuration file permission", err.Error()) |
| 35 | + return err |
| 36 | + } |
| 37 | + log.Println("[INFO] Configuration file copied successfully") |
| 38 | + _ = a.doReportProgress(ProgressTypeConfigComplete, "Configuration Complete") |
| 39 | + return nil |
| 40 | +} |
| 41 | + |
| 42 | +func (a *Agent) launchScriptsConfiguration(typeOf string) error { |
| 43 | + var script, scriptName string |
| 44 | + var reportStart, reportEnd ProgressType |
| 45 | + switch typeOf { |
| 46 | + case "post": |
| 47 | + script = a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.PostConfigurationScript |
| 48 | + scriptName = "post" |
| 49 | + reportStart = ProgressTypePostScriptInitiated |
| 50 | + reportEnd = ProgressTypePostScriptComplete |
| 51 | + default: // pre or default |
| 52 | + script = a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.PreConfigurationScript |
| 53 | + scriptName = "pre" |
| 54 | + reportStart = ProgressTypePreScriptInitiated |
| 55 | + reportEnd = ProgressTypePreScriptComplete |
| 56 | + } |
| 57 | + log.Println("[INFO] Starting the " + scriptName + "-configuration.") |
| 58 | + _ = a.doReportProgress(reportStart, "Report starting") |
| 59 | + // nolint:gosec |
| 60 | + file, err := os.Create(ARTIFACTS_PATH + a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference + scriptName + "configuration.sh") |
| 61 | + if err != nil { |
| 62 | + log.Println("[ERROR] creating the "+scriptName+"-configuration script", err.Error()) |
| 63 | + return err |
| 64 | + } |
| 65 | + defer func() { |
| 66 | + if err := file.Close(); err != nil { |
| 67 | + log.Println("[ERROR] Error when closing:", err) |
| 68 | + } |
| 69 | + }() |
| 70 | + |
| 71 | + plainTest, _ := base64.StdEncoding.DecodeString(script) |
| 72 | + _, err = file.WriteString(string(plainTest)) |
| 73 | + if err != nil { |
| 74 | + log.Println("[ERROR] writing the "+scriptName+"-configuration script", err.Error()) |
| 75 | + return err |
| 76 | + } |
| 77 | + // nolint:gosec |
| 78 | + err = os.Chmod(ARTIFACTS_PATH+a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference+scriptName+"configuration.sh", 0755) |
| 79 | + if err != nil { |
| 80 | + log.Println("[ERROR] changing the "+scriptName+"-configuration script permission", err.Error()) |
| 81 | + return err |
| 82 | + } |
| 83 | + log.Println("[INFO] " + scriptName + "-configuration script created successfully") |
| 84 | + cmd := exec.Command("/bin/sh", ARTIFACTS_PATH+a.BootstrapServerOnboardingInfo.IetfSztpConveyedInfoOnboardingInformation.InfoTimestampReference+scriptName+"configuration.sh") //nolint:gosec |
| 85 | + out, err := cmd.Output() |
| 86 | + if err != nil { |
| 87 | + log.Println("[ERROR] running the "+scriptName+"-configuration script", err.Error()) |
| 88 | + return err |
| 89 | + } |
| 90 | + log.Println(string(out)) // remove it |
| 91 | + _ = a.doReportProgress(reportEnd, "Report end") |
| 92 | + log.Println("[INFO] " + scriptName + "-Configuration script executed successfully") |
| 93 | + return nil |
| 94 | +} |
0 commit comments