ValidateACLJSON takes in the given source and destination (in this situation, it is assumed that you are checking whether the source can connect to destination) and creates an ACLTest from that. It then sends the ACLTest to the control api acl validate endpoint, where the test is run. It returns a n
(ctx context.Context, source, dest string)
| 476 | // validate endpoint, where the test is run. It returns a nil ACLTestError pointer if |
| 477 | // no test errors occur. |
| 478 | func (c *Client) ValidateACLJSON(ctx context.Context, source, dest string) (testErr *ACLTestError, err error) { |
| 479 | // Format return errors to be descriptive. |
| 480 | defer func() { |
| 481 | if err != nil { |
| 482 | err = fmt.Errorf("tailscale.ValidateACLJSON: %w", err) |
| 483 | } |
| 484 | }() |
| 485 | |
| 486 | tests := []ACLTest{{User: source, Allow: []string{dest}}} |
| 487 | postData, err := json.Marshal(tests) |
| 488 | if err != nil { |
| 489 | return nil, err |
| 490 | } |
| 491 | |
| 492 | path := c.BuildTailnetURL("acl", "validate") |
| 493 | req, err := http.NewRequestWithContext(ctx, "POST", path, bytes.NewBuffer(postData)) |
| 494 | if err != nil { |
| 495 | return nil, err |
| 496 | } |
| 497 | |
| 498 | req.Header.Set("Content-Type", "application/json") |
| 499 | c.setAuth(req) |
| 500 | |
| 501 | b, resp, err := c.sendRequest(req) |
| 502 | if err != nil { |
| 503 | return nil, err |
| 504 | } |
| 505 | |
| 506 | if resp.StatusCode != http.StatusOK { |
| 507 | return nil, fmt.Errorf("control api responded with %d status code", resp.StatusCode) |
| 508 | } |
| 509 | |
| 510 | // The test ran without fail |
| 511 | if len(b) == 0 { |
| 512 | return nil, nil |
| 513 | } |
| 514 | |
| 515 | var res ACLTestError |
| 516 | // The test returned errors. |
| 517 | if err = json.Unmarshal(b, &res); err != nil { |
| 518 | // failed to unmarshal |
| 519 | return nil, err |
| 520 | } |
| 521 | return &res, nil |
| 522 | } |
nothing calls this directly
no test coverage detected