https://github.com/bluesky-social/goat but with tangled's CI
9
fork

Configure Feed

Select the types of activity you want to include in your feed.

goat: support both 'Basic' and 'Bearer' relay admin (#1027)

authored by bnewbold.net and committed by

GitHub a97d9bae 03ec018f

+62 -23
+62 -23
relay_admin.go
··· 2 2 3 3 import ( 4 4 "bytes" 5 + "encoding/base64" 5 6 "encoding/json" 6 7 "fmt" 7 8 "io" ··· 18 19 Usage: "sub-comands for relay administration", 19 20 Flags: []cli.Flag{ 20 21 &cli.StringFlag{ 21 - Name: "admin-token", 22 - Required: true, 23 - Usage: "relay admin auth token", 24 - EnvVars: []string{"ATP_AUTH_ADMIN_PASSWORD", "RELAY_ADMIN_TOKEN"}, 22 + Name: "admin-password", 23 + Usage: "relay admin password (for Basic admin auth)", 24 + EnvVars: []string{"RELAY_ADMIN_PASSWORD", "ATP_AUTH_ADMIN_PASSWORD"}, 25 + }, 26 + &cli.StringFlag{ 27 + Name: "admin-bearer-token", 28 + Usage: "relay admin auth token (for Bearer auth)", 29 + EnvVars: []string{"RELAY_ADMIN_BEARER_TOKEN"}, 25 30 }, 26 31 }, 27 32 Subcommands: []*cli.Command{ ··· 144 149 145 150 type RelayAdminClient struct { 146 151 Host string 152 + Password string 147 153 BearerToken string 148 154 } 149 155 ··· 177 183 if err != nil { 178 184 return nil, err 179 185 } 180 - req.Header.Set("Authorization", "Bearer "+c.BearerToken) 186 + if c.Password != "" { 187 + req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:"+c.Password))) 188 + } else if c.BearerToken != "" { 189 + req.Header.Set("Authorization", "Bearer "+c.BearerToken) 190 + } 181 191 req.Header.Set("User-Agent", fmt.Sprintf("goat/"+versioninfo.Short())) 182 192 if buf != nil { 183 193 req.Header.Set("Content-Type", "application/json") ··· 200 210 return respBytes, nil 201 211 } 202 212 203 - func NewRelayAdminClient(cctx *cli.Context) *RelayAdminClient { 204 - // TODO: password-style admin auth 205 - //headers["Authorization"] = "Basic "+base64.StdEncoding.EncodeToString([]byte("admin:"+*c.AdminToken))) 213 + func NewRelayAdminClient(cctx *cli.Context) (*RelayAdminClient, error) { 206 214 client := RelayAdminClient{ 207 215 Host: cctx.String("relay-host"), 208 - BearerToken: cctx.String("admin-token"), 216 + Password: cctx.String("admin-password"), 217 + BearerToken: cctx.String("admin-bearer-token"), 209 218 } 210 - return &client 219 + if client.Password == "" && client.BearerToken == "" { 220 + return nil, fmt.Errorf("either admin password or admin bearer token must be provided") 221 + } 222 + return &client, nil 211 223 } 212 224 213 225 func runRelayAdminAccountTakedown(cctx *cli.Context) error { ··· 222 234 return err 223 235 } 224 236 225 - client := NewRelayAdminClient(cctx) 237 + client, err := NewRelayAdminClient(cctx) 238 + if err != nil { 239 + return err 240 + } 226 241 227 242 path := "/admin/repo/takeDown" 228 243 if cctx.Bool("reverse") { ··· 240 255 } 241 256 242 257 func runRelayAdminAccountList(cctx *cli.Context) error { 243 - client := NewRelayAdminClient(cctx) 258 + client, err := NewRelayAdminClient(cctx) 259 + if err != nil { 260 + return err 261 + } 244 262 path := "/admin/repo/takedowns" 245 263 params := map[string]string{ 246 264 "cursor": "", ··· 274 292 return fmt.Errorf("need to provide hostname as an argument") 275 293 } 276 294 277 - client := NewRelayAdminClient(cctx) 295 + client, err := NewRelayAdminClient(cctx) 296 + if err != nil { 297 + return err 298 + } 278 299 path := "/admin/pds/requestCrawl" 279 300 body := map[string]any{ 280 301 "hostname": hostname, 281 302 } 282 - _, err := client.Do("POST", path, nil, body) 303 + _, err = client.Do("POST", path, nil, body) 283 304 if err != nil { 284 305 return err 285 306 } ··· 293 314 return fmt.Errorf("need to provide hostname as an argument") 294 315 } 295 316 296 - client := NewRelayAdminClient(cctx) 317 + client, err := NewRelayAdminClient(cctx) 318 + if err != nil { 319 + return err 320 + } 297 321 298 322 path := "/admin/pds/block" 299 323 if cctx.Bool("reverse") { ··· 303 327 params := map[string]string{ 304 328 "host": hostname, 305 329 } 306 - _, err := client.Do("POST", path, params, nil) 330 + _, err = client.Do("POST", path, params, nil) 307 331 if err != nil { 308 332 return err 309 333 } ··· 311 335 } 312 336 313 337 func runRelayAdminHostList(cctx *cli.Context) error { 314 - client := NewRelayAdminClient(cctx) 338 + client, err := NewRelayAdminClient(cctx) 339 + if err != nil { 340 + return err 341 + } 315 342 path := "/admin/pds/list" 316 343 317 344 respBytes, err := client.Do("GET", path, nil, nil) ··· 339 366 return fmt.Errorf("need to provide hostname as an argument") 340 367 } 341 368 342 - client := NewRelayAdminClient(cctx) 369 + client, err := NewRelayAdminClient(cctx) 370 + if err != nil { 371 + return err 372 + } 343 373 344 374 path := "/admin/pds/changeLimits" 345 375 ··· 359 389 body["repo_limit"] = cctx.Int("repo-limit") 360 390 } 361 391 362 - _, err := client.Do("POST", path, nil, body) 392 + _, err = client.Do("POST", path, nil, body) 363 393 if err != nil { 364 394 return err 365 395 } ··· 373 403 return fmt.Errorf("need to provide domain as an argument") 374 404 } 375 405 376 - client := NewRelayAdminClient(cctx) 406 + client, err := NewRelayAdminClient(cctx) 407 + if err != nil { 408 + return err 409 + } 377 410 378 411 path := "/admin/subs/banDomain" 379 412 if cctx.Bool("reverse") { ··· 383 416 body := map[string]any{ 384 417 "domain": domain, 385 418 } 386 - _, err := client.Do("POST", path, nil, body) 419 + _, err = client.Do("POST", path, nil, body) 387 420 if err != nil { 388 421 return err 389 422 } ··· 391 424 } 392 425 393 426 func runRelayAdminDomainList(cctx *cli.Context) error { 394 - client := NewRelayAdminClient(cctx) 427 + client, err := NewRelayAdminClient(cctx) 428 + if err != nil { 429 + return err 430 + } 395 431 path := "/admin/subs/listDomainBans" 396 432 397 433 respBytes, err := client.Do("GET", path, nil, nil) ··· 409 445 } 410 446 411 447 func runRelayAdminConsumerList(cctx *cli.Context) error { 412 - client := NewRelayAdminClient(cctx) 448 + client, err := NewRelayAdminClient(cctx) 449 + if err != nil { 450 + return err 451 + } 413 452 path := "/admin/consumers/list" 414 453 415 454 respBytes, err := client.Do("GET", path, nil, nil)