|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/rsa" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/golang-jwt/jwt/v5" |
| 12 | +) |
| 13 | + |
| 14 | +type Actioner struct { |
| 15 | + client *http.Client |
| 16 | + backendURL string |
| 17 | +} |
| 18 | + |
| 19 | +func NewActioner(backendURL string) *Actioner { |
| 20 | + return &Actioner{ |
| 21 | + client: &http.Client{}, |
| 22 | + backendURL: backendURL, |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +type SourceCreate struct { |
| 27 | + Name string `json:"name"` |
| 28 | +} |
| 29 | + |
| 30 | +type SourceResponse struct { |
| 31 | + ID string `json:"id"` |
| 32 | + Name string `json:"name"` |
| 33 | +} |
| 34 | + |
| 35 | +type AssessmentForm struct { |
| 36 | + Name string `json:"name"` |
| 37 | + SourceType string `json:"sourceType"` |
| 38 | + SourceID *string `json:"sourceId,omitempty"` |
| 39 | +} |
| 40 | + |
| 41 | +type AssessmentResponse struct { |
| 42 | + ID string `json:"id"` |
| 43 | + Name string `json:"name"` |
| 44 | +} |
| 45 | + |
| 46 | +func (a *Actioner) CreateSource(name string) (string, error) { |
| 47 | + body := SourceCreate{Name: name} |
| 48 | + data, err := json.Marshal(body) |
| 49 | + if err != nil { |
| 50 | + return "", fmt.Errorf("marshaling request: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + req, err := http.NewRequest(http.MethodPost, a.backendURL+"/api/v1/sources", bytes.NewReader(data)) |
| 54 | + if err != nil { |
| 55 | + return "", fmt.Errorf("creating request: %w", err) |
| 56 | + } |
| 57 | + req.Header.Set("Content-Type", "application/json") |
| 58 | + |
| 59 | + resp, err := a.client.Do(req) |
| 60 | + if err != nil { |
| 61 | + return "", fmt.Errorf("sending request: %w", err) |
| 62 | + } |
| 63 | + defer resp.Body.Close() |
| 64 | + |
| 65 | + if resp.StatusCode != http.StatusCreated { |
| 66 | + return "", fmt.Errorf("unexpected status code: %d", resp.StatusCode) |
| 67 | + } |
| 68 | + |
| 69 | + var source SourceResponse |
| 70 | + if err := json.NewDecoder(resp.Body).Decode(&source); err != nil { |
| 71 | + return "", fmt.Errorf("decoding response: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + return source.ID, nil |
| 75 | +} |
| 76 | + |
| 77 | +func (a *Actioner) DeleteSource(sourceID string) error { |
| 78 | + req, err := http.NewRequest(http.MethodDelete, a.backendURL+"/api/v1/sources/"+sourceID, nil) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("creating request: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + resp, err := a.client.Do(req) |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("sending request: %w", err) |
| 86 | + } |
| 87 | + defer resp.Body.Close() |
| 88 | + |
| 89 | + if resp.StatusCode != http.StatusOK { |
| 90 | + return fmt.Errorf("unexpected status code: %d", resp.StatusCode) |
| 91 | + } |
| 92 | + |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +func (a *Actioner) CreateAssessment(name string, sourceID *string) (string, error) { |
| 97 | + body := AssessmentForm{ |
| 98 | + Name: name, |
| 99 | + SourceType: "agent", |
| 100 | + SourceID: sourceID, |
| 101 | + } |
| 102 | + data, err := json.Marshal(body) |
| 103 | + if err != nil { |
| 104 | + return "", fmt.Errorf("marshaling request: %w", err) |
| 105 | + } |
| 106 | + |
| 107 | + req, err := http.NewRequest(http.MethodPost, a.backendURL+"/api/v1/assessments", bytes.NewReader(data)) |
| 108 | + if err != nil { |
| 109 | + return "", fmt.Errorf("creating request: %w", err) |
| 110 | + } |
| 111 | + req.Header.Set("Content-Type", "application/json") |
| 112 | + |
| 113 | + resp, err := a.client.Do(req) |
| 114 | + if err != nil { |
| 115 | + return "", fmt.Errorf("sending request: %w", err) |
| 116 | + } |
| 117 | + defer resp.Body.Close() |
| 118 | + |
| 119 | + if resp.StatusCode != http.StatusCreated { |
| 120 | + return "", fmt.Errorf("unexpected status code: %d", resp.StatusCode) |
| 121 | + } |
| 122 | + |
| 123 | + var assessment AssessmentResponse |
| 124 | + if err := json.NewDecoder(resp.Body).Decode(&assessment); err != nil { |
| 125 | + return "", fmt.Errorf("decoding response: %w", err) |
| 126 | + } |
| 127 | + |
| 128 | + return assessment.ID, nil |
| 129 | +} |
| 130 | + |
| 131 | +func (a *Actioner) GenerateAgentToken(sourceID, kid string, privateKey *rsa.PrivateKey) (string, error) { |
| 132 | + type AgentTokenClaims struct { |
| 133 | + SourceID string `json:"source_id"` |
| 134 | + jwt.RegisteredClaims |
| 135 | + } |
| 136 | + |
| 137 | + claims := AgentTokenClaims{ |
| 138 | + sourceID, |
| 139 | + jwt.RegisteredClaims{ |
| 140 | + ExpiresAt: jwt.NewNumericDate(time.Now().Add(30 * time.Hour)), |
| 141 | + IssuedAt: jwt.NewNumericDate(time.Now()), |
| 142 | + NotBefore: jwt.NewNumericDate(time.Now()), |
| 143 | + Issuer: "assisted-migrations", |
| 144 | + Subject: sourceID, |
| 145 | + ID: "1", |
| 146 | + Audience: []string{"assisted-migrations"}, |
| 147 | + }, |
| 148 | + } |
| 149 | + |
| 150 | + token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims) |
| 151 | + token.Header["kid"] = kid |
| 152 | + signedToken, err := token.SignedString(privateKey) |
| 153 | + if err != nil { |
| 154 | + return "", fmt.Errorf("failed to sign agent token: %w", err) |
| 155 | + } |
| 156 | + |
| 157 | + return signedToken, nil |
| 158 | +} |
0 commit comments