MCPcopy
hub / github.com/ContainerSSH/ContainerSSH / generateHostKeys

Function generateHostKeys

main.go:238–292  ·  view source on GitHub ↗
(configFile string, cfg *config.AppConfig, logger log.Logger)

Source from the content-addressed store, hash-verified

236}
237
238func generateHostKeys(configFile string, cfg *config.AppConfig, logger log.Logger) error {
239 if err := cfg.SSH.GenerateHostKey(); err != nil {
240 return err
241 }
242
243 tmpFile := fmt.Sprintf("%s~", configFile)
244 fh, err := os.Create(tmpFile)
245 if err != nil {
246 logger.Warning(
247 message.Wrap(
248 err,
249 message.ECannotWriteConfigFile,
250 "Cannot create temporary configuration file at %s with updated host keys.",
251 tmpFile,
252 ).Label("tmpFile", configFile))
253 return nil
254 }
255 format := getConfigFileFormat(configFile)
256 saver, err := internalConfig.NewWriterSaver(fh, logger, format)
257 if err != nil {
258 _ = fh.Close()
259 logger.Warning(
260 message.Wrap(
261 err,
262 message.ECannotWriteConfigFile,
263 "Cannot initialize temporary configuration file at %s with updated host keys.",
264 tmpFile,
265 ).Label("tmpFile", configFile))
266 return nil
267 }
268 if err := saver.Save(cfg); err != nil {
269 _ = fh.Close()
270 logger.Warning(
271 message.Wrap(
272 err,
273 message.ECannotWriteConfigFile,
274 "Cannot save temporary configuration file at %s with updated host keys.",
275 tmpFile,
276 ).Label("tmpFile", configFile))
277 return nil
278 }
279 if err := fh.Close(); err != nil {
280 logger.Warning(message.Wrap(err,
281 message.ECannotWriteConfigFile, "Cannot close temporary configuration file at %s with updated host keys.", tmpFile).Label("tmpFile", configFile))
282 return nil
283 }
284
285 if err := os.Rename(tmpFile, configFile); err != nil {
286 logger.Warning(message.Wrap(err,
287 message.ECannotWriteConfigFile, "Failed to rename temporary file %s to %s with updated host keys.", tmpFile, configFile).Label("file", configFile).Label("tmpFile", tmpFile))
288 return fmt.Errorf("failed to rename temporary file %s to %s (%w)", tmpFile, configFile, err)
289 }
290
291 return nil
292}
293
294func healthCheck(cfg config.AppConfig, logger log.Logger) error {
295 healthClient, err := health.NewClient(cfg.Health, logger)

Callers 1

runContainerSSHFunction · 0.85

Calls 8

WrapFunction · 0.92
getConfigFileFormatFunction · 0.85
GenerateHostKeyMethod · 0.80
WarningMethod · 0.65
LabelMethod · 0.65
CloseMethod · 0.65
SaveMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected