StartClient starts a client instance with a client config and a user agent
(config *config.ClientConfig, userAgent string)
| 17 | |
| 18 | // StartClient starts a client instance with a client config and a user agent |
| 19 | func StartClient(config *config.ClientConfig, userAgent string) { |
| 20 | log.Infof("Initializing client") |
| 21 | |
| 22 | // Check if the config is valid |
| 23 | err := validateConfig(config) |
| 24 | |
| 25 | if err != nil { |
| 26 | log.Warnf("Configuration error: %v", err) |
| 27 | os.Exit(1) |
| 28 | } |
| 29 | |
| 30 | log.Infof("Configuring connection to %s for gRPC operations", config.GetDialAddr()) |
| 31 | |
| 32 | // Configure connection |
| 33 | // TODO: Use TLS for encrypting connection (with configurable certs and a sane default) |
| 34 | conn, err := grpc.Dial( |
| 35 | config.GetDialAddr(), |
| 36 | grpc.WithBackoffConfig(grpc.DefaultBackoffConfig), |
| 37 | grpc.WithInsecure(), |
| 38 | grpc.WithUserAgent(userAgent+";"), |
| 39 | ) |
| 40 | |
| 41 | if err != nil { |
| 42 | log.Warnf("Could not configure connection to host: %v", err) |
| 43 | os.Exit(1) |
| 44 | } |
| 45 | |
| 46 | defer conn.Close() |
| 47 | |
| 48 | // Create client from connection |
| 49 | client := api.NewCertificateIssuerClient(conn) |
| 50 | |
| 51 | // Test connection |
| 52 | _, err = client.Ping(context.Background(), &api.PingRequest{Msg: "ping"}) |
| 53 | |
| 54 | if err != nil { |
| 55 | log.Warnf("Could not test connection to %s: %v", config.GetDialAddr(), err) |
| 56 | os.Exit(1) |
| 57 | } |
| 58 | |
| 59 | log.Infof("Successfully pinged intercert host") |
| 60 | |
| 61 | // Create storage for saving/loading certs |
| 62 | certStorage := NewCertStorage(config.GetCertStorage()) |
| 63 | |
| 64 | // Configure cert workers |
| 65 | for w := 1; w <= runtime.NumCPU(); w++ { |
| 66 | go CertWorker(w, client, certStorage) |
| 67 | } |
| 68 | |
| 69 | // Set up scheduled tasks |
| 70 | tasks := configureTasks(client, config, certStorage) |
| 71 | |
| 72 | // Handle termination |
| 73 | configureTermination(tasks) |
| 74 | |
| 75 | log.Infof("Client running - ready to work!") |
| 76 |
no test coverage detected