|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "os/signal" |
| 8 | + "path/filepath" |
| 9 | + "syscall" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/mitchellh/go-ps" |
| 13 | + "github.com/spf13/cobra" |
| 14 | + "google.golang.org/protobuf/types/known/emptypb" |
| 15 | + "gopkg.in/yaml.v3" |
| 16 | +) |
| 17 | + |
| 18 | +var serviceCmd = &cobra.Command{ |
| 19 | + Use: "service", |
| 20 | + Short: "Service keeping nebula configuration up to date", |
| 21 | + Run: func(cmd *cobra.Command, args []string) { |
| 22 | + agent, err := NewClient(l, config) |
| 23 | + if err != nil { |
| 24 | + l.WithError(err).Error("failed to create client") |
| 25 | + os.Exit(1) |
| 26 | + } |
| 27 | + defer agent.Close() |
| 28 | + |
| 29 | + templatePath := config.GetString("service.config_template", "nebula.yml.template") |
| 30 | + outputPath := config.GetString("service.config_output", "/etc/nebula/") |
| 31 | + |
| 32 | + templatePath = filepath.Join(configDir, templatePath) |
| 33 | + if ok, _ := fileExists(templatePath); !ok { |
| 34 | + l.Errorf("file not found: %s", templatePath) |
| 35 | + os.Exit(1) |
| 36 | + } |
| 37 | + |
| 38 | + interval := config.GetDuration("service.interval", 10*time.Minute) |
| 39 | + |
| 40 | + ticker := time.NewTicker(interval) |
| 41 | + sighup := make(chan os.Signal, 1) |
| 42 | + signal.Notify(sighup, syscall.SIGHUP) |
| 43 | + |
| 44 | + quit := make(chan struct{}) |
| 45 | + go func() { |
| 46 | + run(agent, templatePath, outputPath) |
| 47 | + for { |
| 48 | + select { |
| 49 | + case <-sighup: |
| 50 | + l.Debug("received HUP signal, triggering update") |
| 51 | + run(agent, templatePath, outputPath) |
| 52 | + case <-ticker.C: |
| 53 | + l.Debug("received tick, triggering update") |
| 54 | + run(agent, templatePath, outputPath) |
| 55 | + case <-quit: |
| 56 | + ticker.Stop() |
| 57 | + return |
| 58 | + } |
| 59 | + } |
| 60 | + }() |
| 61 | + |
| 62 | + serviceShutdownBlock() |
| 63 | + }, |
| 64 | +} |
| 65 | + |
| 66 | +func init() { |
| 67 | +} |
| 68 | + |
| 69 | +func serviceShutdownBlock() { |
| 70 | + sigChan := make(chan os.Signal) |
| 71 | + signal.Notify(sigChan, syscall.SIGTERM) |
| 72 | + signal.Notify(sigChan, syscall.SIGINT) |
| 73 | + |
| 74 | + select { |
| 75 | + case rawSig := <-sigChan: |
| 76 | + sig := rawSig.String() |
| 77 | + l.WithField("signal", sig).Info("Caught signal, shutting down") |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func run(agent *agentClient, configTemplatePath, outputPath string) { |
| 82 | + l.Infoln("Generate config dir") |
| 83 | + |
| 84 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 85 | + defer cancel() |
| 86 | + |
| 87 | + status, err := agent.client.GetEnrollStatus(ctx, &emptypb.Empty{}) |
| 88 | + if err != nil { |
| 89 | + l.WithError(err).Errorf("failed to get enrollment status") |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + if !status.IsEnrolled { |
| 94 | + l.Info("agent is not enrolled yet") |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + var nebulaConfig map[interface{}]interface{} |
| 99 | + bs, err := ioutil.ReadFile(configTemplatePath) |
| 100 | + if err != nil { |
| 101 | + l.WithError(err).Errorf("failed to read template file") |
| 102 | + return |
| 103 | + } |
| 104 | + if err := yaml.Unmarshal(bs, &nebulaConfig); err != nil { |
| 105 | + l.WithError(err).Errorf("error when reading contents of %s", configTemplatePath) |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + _, err = os.Stat(outputPath) |
| 110 | + if err != nil { |
| 111 | + if os.IsNotExist(err) { |
| 112 | + err = os.Mkdir(outputPath, 0700) |
| 113 | + if err != nil { |
| 114 | + l.WithError(err).Errorf("failed to create directory `%s`\n", outputPath) |
| 115 | + return |
| 116 | + } |
| 117 | + } else { |
| 118 | + l.WithError(err).Errorf("failed to create directory `%s`\n", outputPath) |
| 119 | + return |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + caBytes := make([]byte, 0) |
| 124 | + for i := range status.CertificateAuthorities { |
| 125 | + caBytes = append(caBytes, status.CertificateAuthorities[i].PublicKeyPEM...) |
| 126 | + caBytes = append(caBytes, '\n') |
| 127 | + } |
| 128 | + |
| 129 | + caFilePath := filepath.Join(outputPath, "ca.crt") |
| 130 | + if err := ioutil.WriteFile(caFilePath, caBytes, 0600); err != nil { |
| 131 | + l.WithError(err).Errorf("failed to write ca.crt to `%s`\n", caFilePath) |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + if _, ok := nebulaConfig["pki"]; !ok { |
| 136 | + nebulaConfig["pki"] = make(map[string]interface{}) |
| 137 | + } |
| 138 | + pki := nebulaConfig["pki"].(map[string]interface{}) |
| 139 | + pki["ca"], err = filepath.Abs(caFilePath) |
| 140 | + if err != nil { |
| 141 | + l.WithError(err).Errorf("failed to get absolut path for `%s`\n", caFilePath) |
| 142 | + return |
| 143 | + } |
| 144 | + if ok, _ := fileExists(pki["ca"].(string)); !ok { |
| 145 | + l.WithError(err).Errorf("file not found `%s`\n", pki["ca"].(string)) |
| 146 | + return |
| 147 | + } |
| 148 | + |
| 149 | + crtFilePath := filepath.Join(outputPath, "nebula.crt") |
| 150 | + if err := ioutil.WriteFile(crtFilePath, []byte(status.SignedPEM), 0600); err != nil { |
| 151 | + l.WithError(err).Errorf("failed to write nebula.crt to `%s`\n", crtFilePath) |
| 152 | + return |
| 153 | + } |
| 154 | + pki["cert"], err = filepath.Abs(crtFilePath) |
| 155 | + if err != nil { |
| 156 | + l.WithError(err).Errorf("failed to get absolut path for `%s`\n", crtFilePath) |
| 157 | + return |
| 158 | + } |
| 159 | + if ok, _ := fileExists(pki["cert"].(string)); !ok { |
| 160 | + l.WithError(err).Errorf("file not found `%s`\n", pki["cert"].(string)) |
| 161 | + return |
| 162 | + } |
| 163 | + |
| 164 | + keyPath := resolvePath(AgentNebulaKeyPath) |
| 165 | + if ok, _ := fileExists(keyPath); !ok { |
| 166 | + l.Errorf("nebula key not exists: %s", keyPath) |
| 167 | + return |
| 168 | + } |
| 169 | + |
| 170 | + keyFileSource, err := ioutil.ReadFile(keyPath) |
| 171 | + if err != nil { |
| 172 | + l.WithError(err).Errorf("error open file: %s", keyPath) |
| 173 | + return |
| 174 | + } |
| 175 | + |
| 176 | + keyPathDst := filepath.Join(outputPath, "nebula.key") |
| 177 | + if ok, info := fileExists(keyPathDst); ok && info.Mode() != 0600 { |
| 178 | + l.Warnf("wrong permission on key %s, fixing...", keyPathDst) |
| 179 | + err = os.Chmod(keyPathDst, 0600) |
| 180 | + if err != nil { |
| 181 | + l.WithError(err).Errorf("failed to fix permission on %s", keyPathDst) |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + if err := ioutil.WriteFile(keyPathDst, keyFileSource, 0600); err != nil { |
| 186 | + l.WithError(err).Errorf("failed to write nebula.key to `%s`\n", keyPathDst) |
| 187 | + return |
| 188 | + } |
| 189 | + |
| 190 | + pki["key"], err = filepath.Abs(keyPathDst) |
| 191 | + if err != nil { |
| 192 | + l.WithError(err).Errorf("failed to get absolut path for `%s`", keyPathDst) |
| 193 | + return |
| 194 | + } |
| 195 | + if ok, _ := fileExists(pki["key"].(string)); !ok { |
| 196 | + l.WithError(err).Errorf("file not found `%s`\n", pki["key"].(string)) |
| 197 | + return |
| 198 | + } |
| 199 | + |
| 200 | + blockedFingerprints := make([]string, 0) |
| 201 | + |
| 202 | + for _, crl := range status.CertificateRevocationList { |
| 203 | + blockedFingerprints = append(blockedFingerprints, crl.Fingerprints...) |
| 204 | + } |
| 205 | + |
| 206 | + pki["blocklist"] = blockedFingerprints |
| 207 | + |
| 208 | + bs, err = yaml.Marshal(nebulaConfig) |
| 209 | + if err != nil { |
| 210 | + l.WithError(err).Error("failed to generate config") |
| 211 | + return |
| 212 | + } |
| 213 | + configFilePath := filepath.Join(outputPath, "config.yml") |
| 214 | + if err := ioutil.WriteFile(configFilePath, bs, 0600); err != nil { |
| 215 | + l.WithError(err).Errorf("failed to write config file to `%s`\n", configFilePath) |
| 216 | + return |
| 217 | + } |
| 218 | + |
| 219 | + reloadNebula() |
| 220 | +} |
| 221 | + |
| 222 | +func reloadNebula() { |
| 223 | + processes, err := ps.Processes() |
| 224 | + if err != nil { |
| 225 | + l.WithError(err).Error("failed to get os processes") |
| 226 | + return |
| 227 | + } |
| 228 | + |
| 229 | + for i := range processes { |
| 230 | + if processes[i].Executable() == "nebula" { |
| 231 | + l.Infof("nebula pid is %d", processes[i].Pid()) |
| 232 | + err = syscall.Kill(processes[i].Pid(), syscall.SIGHUP) |
| 233 | + if err != nil { |
| 234 | + l.WithError(err).Errorf("failure when trying to trigger nebula reload") |
| 235 | + } |
| 236 | + break |
| 237 | + } |
| 238 | + } |
| 239 | +} |
0 commit comments