mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-21 20:22:41 +09:00
ipn,feature/acme,client/local: add tests for the cert-status localapi endpoint
Tests that the localapi cert-status/ handler checks read access and returns the status as JSON, that LocalBackend.CertStatus goes through the hook, and that feature/acme's hook reports the same missing, valid and expired results as the C2N /tls-cert-status handler. Also tests that Client.CertStatus returns ErrCertStatusUnsupported for a 404 and not for other errors. Updates tailscale/corp#38175 Signed-off-by: chaosinthecrd <tom@tmlabs.co.uk>
This commit is contained in:
1 parent
a986bbfac2
commit
408f0d2b36
4 files changed
+169
No files matched your search
@@ -0,0 +1,52 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
//go:build !js && !ts_omit_acme
|
||||
|
||||
package local
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"tailscale.com/tstest/nettest"
|
||||
)
|
||||
|
||||
func TestCertStatusUnsupported(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
status int
|
||||
wantUnsupported bool
|
||||
}{
|
||||
{name: "not_found", status: http.StatusNotFound, wantUnsupported: true},
|
||||
{name: "server_error", status: http.StatusInternalServerError, wantUnsupported: false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
nw := nettest.GetNetwork(t)
|
||||
ts := nettest.NewHTTPServer(nw, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(tt.status)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
lc := &Client{
|
||||
Dial: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||
return nw.Dial(ctx, network, ts.Listener.Addr().String())
|
||||
},
|
||||
}
|
||||
_, err := lc.CertStatus(context.Background(), "node.example.ts.net")
|
||||
if err == nil {
|
||||
t.Fatal("CertStatus: got nil error")
|
||||
}
|
||||
if got := errors.Is(err, ErrCertStatusUnsupported); got != tt.wantUnsupported {
|
||||
t.Errorf("errors.Is(%v, ErrCertStatusUnsupported) = %v, want %v", err, got, tt.wantUnsupported)
|
||||
}
|
||||
if got := errors.Is(err, errors.ErrUnsupported); got != tt.wantUnsupported {
|
||||
t.Errorf("errors.Is(%v, errors.ErrUnsupported) = %v, want %v", err, got, tt.wantUnsupported)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -125,6 +125,14 @@ func TestHandleC2NTLSCertStatus(t *testing.T) {
|
||||
if !reflect.DeepEqual(&got, tt.want) {
|
||||
t.Errorf("got %v; want %v", logger.AsJSON(got), logger.AsJSON(tt.want))
|
||||
}
|
||||
// The localapi cert-status endpoint reads the same cache via LocalBackend.CertStatus.
|
||||
gotLocal, err := b.CertStatus(tt.domain)
|
||||
if err != nil {
|
||||
t.Fatalf("CertStatus: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(gotLocal, tt.want) {
|
||||
t.Errorf("CertStatus = %v; want %v", logger.AsJSON(gotLocal), logger.AsJSON(tt.want))
|
||||
}
|
||||
} else if tt.wantError != "" {
|
||||
if got := rec.Body.String(); got != tt.wantError {
|
||||
t.Errorf("body = %q; want %q", got, tt.wantError)
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package ipnlocal
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"tailscale.com/tailcfg"
|
||||
)
|
||||
|
||||
func TestCertStatus(t *testing.T) {
|
||||
b := &LocalBackend{}
|
||||
|
||||
if _, err := b.CertStatus("node.example.ts.net"); !errors.Is(err, errNoCerts) {
|
||||
t.Fatalf("CertStatus without ACME support: err = %v, want %v", err, errNoCerts)
|
||||
}
|
||||
|
||||
want := &tailcfg.C2NTLSCertInfo{Missing: true, Error: "no certificate"}
|
||||
var gotDomain string
|
||||
t.Cleanup(HookCertStatus.SetForTest(func(hb *LocalBackend, domain string) (*tailcfg.C2NTLSCertInfo, error) {
|
||||
if hb != b {
|
||||
t.Errorf("hook called with a different LocalBackend")
|
||||
}
|
||||
gotDomain = domain
|
||||
return want, nil
|
||||
}))
|
||||
got, err := b.CertStatus("node.example.ts.net")
|
||||
if err != nil {
|
||||
t.Fatalf("CertStatus: %v", err)
|
||||
}
|
||||
if got != want {
|
||||
t.Errorf("CertStatus = %+v, want %+v", got, want)
|
||||
}
|
||||
if gotDomain != "node.example.ts.net" {
|
||||
t.Errorf("hook got domain %q, want %q", gotDomain, "node.example.ts.net")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
//go:build !ios && !android && !js && !ts_omit_acme
|
||||
|
||||
package localapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"tailscale.com/ipn/ipnlocal"
|
||||
"tailscale.com/tailcfg"
|
||||
)
|
||||
|
||||
func TestServeCertStatus(t *testing.T) {
|
||||
valid := &tailcfg.C2NTLSCertInfo{
|
||||
Valid: true,
|
||||
NotBefore: "2026-01-01T00:00:00Z",
|
||||
NotAfter: "2026-04-01T00:00:00Z",
|
||||
}
|
||||
missing := &tailcfg.C2NTLSCertInfo{Missing: true, Error: "no certificate"}
|
||||
t.Cleanup(ipnlocal.HookCertStatus.SetForTest(func(_ *ipnlocal.LocalBackend, domain string) (*tailcfg.C2NTLSCertInfo, error) {
|
||||
switch domain {
|
||||
case "valid.example.ts.net":
|
||||
return valid, nil
|
||||
case "broken.example.ts.net":
|
||||
return nil, errors.New("cert store unavailable")
|
||||
}
|
||||
return missing, nil
|
||||
}))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
permitRead bool
|
||||
domain string
|
||||
wantCode int
|
||||
want *tailcfg.C2NTLSCertInfo
|
||||
}{
|
||||
{"valid", true, "valid.example.ts.net", http.StatusOK, valid},
|
||||
{"missing", true, "missing.example.ts.net", http.StatusOK, missing},
|
||||
{"store_error", true, "broken.example.ts.net", http.StatusInternalServerError, nil},
|
||||
{"forbidden", false, "valid.example.ts.net", http.StatusForbidden, nil},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
h := handlerForTest(t, &Handler{PermitRead: tt.permitRead})
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("GET", "/localapi/v0/cert-status/"+tt.domain, nil)
|
||||
h.serveCertStatus(rec, req)
|
||||
if rec.Code != tt.wantCode {
|
||||
t.Fatalf("status = %d, want %d; body=%q", rec.Code, tt.wantCode, rec.Body.String())
|
||||
}
|
||||
if tt.want == nil {
|
||||
return
|
||||
}
|
||||
var got tailcfg.C2NTLSCertInfo
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
|
||||
t.Fatalf("unmarshal body %q: %v", rec.Body.Bytes(), err)
|
||||
}
|
||||
if !reflect.DeepEqual(&got, tt.want) {
|
||||
t.Errorf("got %+v, want %+v", got, *tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user