Skeleton contracts / contracts (pull_request) Blocked by required conditions
Smoke/security/grade4/persistence/failover пройдены; REPORT и DEFENSE.
127 lines
3.1 KiB
Go
127 lines
3.1 KiB
Go
package openbao
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// VerifyAppRole proves service authentication against OpenBao (no root token in app).
|
|
func VerifyAppRole(ctx context.Context, role string) error {
|
|
addr := strings.TrimRight(os.Getenv("BAO_ADDR"), "/")
|
|
if addr == "" {
|
|
return nil
|
|
}
|
|
roleID, err := readFile(os.Getenv("BAO_ROLE_ID_FILE"))
|
|
if err != nil || strings.HasPrefix(roleID, "00000000") || roleID == "bootstrap-pending" {
|
|
return fmt.Errorf("openbao approle not bootstrapped")
|
|
}
|
|
secretID, err := readFile(os.Getenv("BAO_SECRET_ID_FILE"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
caPath := os.Getenv("BAO_CACERT")
|
|
if caPath == "" {
|
|
caPath = "/run/secrets/ca_cert"
|
|
}
|
|
client, err := httpClient(caPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
body := fmt.Sprintf(`{"role_id":%q,"secret_id":%q}`, roleID, secretID)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, addr+"/v1/auth/approle/login", strings.NewReader(body))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("approle login: %w", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode != http.StatusOK {
|
|
b, _ := io.ReadAll(resp.Body)
|
|
return fmt.Errorf("approle login status %d: %s", resp.StatusCode, strings.TrimSpace(string(b)))
|
|
}
|
|
var login struct {
|
|
Auth struct {
|
|
ClientToken string `json:"client_token"`
|
|
} `json:"auth"`
|
|
}
|
|
if err := json.NewDecoder(resp.Body).Decode(&login); err != nil {
|
|
return err
|
|
}
|
|
token := login.Auth.ClientToken
|
|
if token == "" {
|
|
return fmt.Errorf("empty openbao token")
|
|
}
|
|
|
|
path := os.Getenv("BAO_VERIFY_KV_PATH")
|
|
if path == "" {
|
|
if role == "worker" {
|
|
path = "messenger/worker/stand"
|
|
} else {
|
|
path = "messenger/api/stand"
|
|
}
|
|
}
|
|
req2, err := http.NewRequestWithContext(ctx, http.MethodGet, addr+"/v1/secret/data/"+path, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req2.Header.Set("X-Vault-Token", token)
|
|
resp2, err := client.Do(req2)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp2.Body.Close()
|
|
if resp2.StatusCode == http.StatusNotFound {
|
|
return nil
|
|
}
|
|
if resp2.StatusCode != http.StatusOK {
|
|
b, _ := io.ReadAll(resp2.Body)
|
|
return fmt.Errorf("kv read status %d: %s", resp2.StatusCode, strings.TrimSpace(string(b)))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func readFile(path string) (string, error) {
|
|
if path == "" {
|
|
return "", fmt.Errorf("empty path")
|
|
}
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return strings.TrimSpace(string(b)), nil
|
|
}
|
|
|
|
func httpClient(caPath string) (*http.Client, error) {
|
|
caPEM, err := os.ReadFile(caPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pool := x509.NewCertPool()
|
|
if !pool.AppendCertsFromPEM(caPEM) {
|
|
return nil, fmt.Errorf("parse bao ca")
|
|
}
|
|
serverName := os.Getenv("BAO_TLS_SERVER_NAME")
|
|
if serverName == "" {
|
|
serverName = "openbao.messenger.lab"
|
|
}
|
|
tr := &http.Transport{
|
|
TLSClientConfig: &tls.Config{
|
|
RootCAs: pool,
|
|
MinVersion: tls.VersionTLS12,
|
|
ServerName: serverName,
|
|
},
|
|
}
|
|
return &http.Client{Transport: tr, Timeout: 5 * time.Second}, nil
|
|
} |