|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/tls" |
| 6 | + "crypto/x509" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "log" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +// newKeylimeClient creates HTTP client for Keylime API with mTLS support |
| 16 | +func newKeylimeClient(baseURL string) *KeylimeClient { |
| 17 | + // Remove "http(s)://" prefix if present |
| 18 | + baseURL = strings.TrimPrefix(baseURL, "https://") |
| 19 | + baseURL = strings.TrimPrefix(baseURL, "http://") |
| 20 | + |
| 21 | + var finalURL string |
| 22 | + var httpClient *http.Client |
| 23 | + |
| 24 | + if config.TLSEnabled { |
| 25 | + finalURL = "https://" + strings.TrimSuffix(baseURL, "/") |
| 26 | + tlsConfig := createTLSConfig() |
| 27 | + httpClient = &http.Client{ |
| 28 | + Transport: &http.Transport{ |
| 29 | + TLSClientConfig: tlsConfig, |
| 30 | + }, |
| 31 | + } |
| 32 | + } else { |
| 33 | + finalURL = "http://" + strings.TrimSuffix(baseURL, "/") |
| 34 | + httpClient = &http.Client{} |
| 35 | + } |
| 36 | + |
| 37 | + return &KeylimeClient{ |
| 38 | + baseURL: finalURL, |
| 39 | + apiVersion: config.APIVersion, |
| 40 | + httpClient: httpClient, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// createTLSConfig creates TLS configuration with mTLS support |
| 45 | +// Equivalent to Python's HostNameIgnoreAdapter with SSL context |
| 46 | +func createTLSConfig() *tls.Config { |
| 47 | + // Load client certificate and key |
| 48 | + cert, err := tls.LoadX509KeyPair(config.ClientCert, config.ClientKey) |
| 49 | + if err != nil { |
| 50 | + log.Printf("Warning: Failed to load client certificate: %v", err) |
| 51 | + log.Printf("Attempting to connect without client cert (may fail with mTLS servers)") |
| 52 | + // Return basic TLS config without client cert |
| 53 | + return &tls.Config{ |
| 54 | + InsecureSkipVerify: true, |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Load CA certificate |
| 59 | + caCertPEM, err := os.ReadFile(config.CAPath) |
| 60 | + if err != nil { |
| 61 | + log.Printf("Warning: Failed to load CA certificate: %v", err) |
| 62 | + log.Printf("Using system CA pool") |
| 63 | + } |
| 64 | + |
| 65 | + // Create CA pool |
| 66 | + caCertPool := x509.NewCertPool() |
| 67 | + if caCertPEM != nil { |
| 68 | + if !caCertPool.AppendCertsFromPEM(caCertPEM) { |
| 69 | + log.Printf("Warning: Failed to append CA certificate to pool") |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + tlsConfig := &tls.Config{ |
| 74 | + Certificates: []tls.Certificate{cert}, |
| 75 | + RootCAs: caCertPool, |
| 76 | + // Ignore hostname verification (like Python's HostNameIgnoreAdapter) |
| 77 | + // This is needed because Keylime certs often don't have correct hostname |
| 78 | + InsecureSkipVerify: config.IgnoreHostname, |
| 79 | + } |
| 80 | + |
| 81 | + return tlsConfig |
| 82 | +} |
| 83 | + |
| 84 | +func (kc *KeylimeClient) Get(endpoint string) (*http.Response, error) { |
| 85 | + url := fmt.Sprintf("%s/%s/%s", kc.baseURL, kc.apiVersion, strings.TrimPrefix(endpoint, "/")) |
| 86 | + return kc.httpClient.Get(url) |
| 87 | +} |
| 88 | + |
| 89 | +func (kc *KeylimeClient) Post(endpoint string, body interface{}) (*http.Response, error) { |
| 90 | + url := fmt.Sprintf("%s/%s/%s", kc.baseURL, kc.apiVersion, strings.TrimPrefix(endpoint, "/")) |
| 91 | + var buf bytes.Buffer |
| 92 | + if body != nil { |
| 93 | + if err := json.NewEncoder(&buf).Encode(body); err != nil { |
| 94 | + return nil, fmt.Errorf("failed to marshal body: %w", err) |
| 95 | + } |
| 96 | + } |
| 97 | + req, err := http.NewRequest("POST", url, &buf) |
| 98 | + if err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + req.Header.Set("Content-Type", "application/json") |
| 102 | + return kc.httpClient.Do(req) |
| 103 | +} |
| 104 | + |
| 105 | +func (kc *KeylimeClient) Put(endpoint string, body interface{}) (*http.Response, error) { |
| 106 | + url := fmt.Sprintf("%s/%s/%s", kc.baseURL, kc.apiVersion, strings.TrimPrefix(endpoint, "/")) |
| 107 | + var buf bytes.Buffer |
| 108 | + if body != nil { |
| 109 | + if err := json.NewEncoder(&buf).Encode(body); err != nil { |
| 110 | + return nil, fmt.Errorf("failed to marshal body: %w", err) |
| 111 | + } |
| 112 | + } |
| 113 | + req, err := http.NewRequest("PUT", url, &buf) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + req.Header.Set("Content-Type", "application/json") |
| 118 | + return kc.httpClient.Do(req) |
| 119 | +} |
| 120 | + |
| 121 | +func (kc *KeylimeClient) Delete(endpoint string) (*http.Response, error) { |
| 122 | + url := fmt.Sprintf("%s/%s/%s", kc.baseURL, kc.apiVersion, strings.TrimPrefix(endpoint, "/")) |
| 123 | + req, err := http.NewRequest("DELETE", url, nil) |
| 124 | + if err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + return kc.httpClient.Do(req) |
| 128 | +} |
0 commit comments