MCPcopy Index your code
hub / github.com/tailscale/tailscale / ValidateACLJSON

Method ValidateACLJSON

client/tailscale/acl.go:478–522  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

476// validate endpoint, where the test is run. It returns a nil ACLTestError pointer if
477// no test errors occur.
478func (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}

Callers

nothing calls this directly

Calls 6

BuildTailnetURLMethod · 0.95
setAuthMethod · 0.95
sendRequestMethod · 0.95
ErrorfMethod · 0.65
MarshalMethod · 0.65
SetMethod · 0.65

Tested by

no test coverage detected