One-view tokens: local records in linked zones, zone lookup, view list
A token for one view of a linked zone (an ACME token for external) couldn't add records: a new record was shared and reached the other view. A new name and type is now local when the user may not change it in the other views. A zone name in several views resolves to the only one the user can see, and the error no longer names views they can't. GET /views lists the views a user sees zones in (name, colour, count; limited) without views:read. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4cc2ab2287
commit
57012fece8
13 files changed
+159
-12
No files matched your search
@@ -76,11 +76,13 @@ func (x *expander) resolveZone(zoneID int64, zone, view string) (*model.Zone, er
|
||||
if zone == "" {
|
||||
return nil, errf(KindBadRequest, "the zone is missing (zone_id or zone)")
|
||||
}
|
||||
return FindZoneRef(x.st, zone, view)
|
||||
return FindZoneRef(x.st, zone, view, func(z *model.Zone) bool { return x.p.CanSeeZone(z.Name, x.st.ViewName(z)) })
|
||||
}
|
||||
|
||||
// FindZoneRef finds a zone by name and optional view name.
|
||||
func FindZoneRef(st *model.State, zone, view string) (*model.Zone, error) {
|
||||
// FindZoneRef finds a zone by name and optional view name. Without a
|
||||
// view, a name in several views needs one, counting only the zones sees
|
||||
// allows (nil: all): a token for one view finds that view's zone.
|
||||
func FindZoneRef(st *model.State, zone, view string, sees func(*model.Zone) bool) (*model.Zone, error) {
|
||||
name, err := ParseZoneName(zone)
|
||||
if err != nil {
|
||||
return nil, errf(KindBadRequest, "%v", err)
|
||||
@@ -97,6 +99,16 @@ func FindZoneRef(st *model.State, zone, view string) (*model.Zone, error) {
|
||||
return z, nil
|
||||
}
|
||||
zs := st.ZonesNamed(name)
|
||||
if sees != nil && len(zs) > 1 {
|
||||
var vis []*model.Zone
|
||||
for _, z := range zs {
|
||||
if sees(z) {
|
||||
vis = append(vis, z)
|
||||
}
|
||||
}
|
||||
zs = vis // none: not found, without naming the views
|
||||
}
|
||||
st.SortByView(zs)
|
||||
switch len(zs) {
|
||||
case 0:
|
||||
return nil, errf(KindNotFound, "zone %s does not exist", dnsname.Display(name))
|
||||
|
||||
@@ -456,12 +456,30 @@ func (x *expander) unlinkOps(m *model.Zone, summary string) []model.Op {
|
||||
|
||||
// localFor works out whether a new record (or set) in zone z is local:
|
||||
// as asked, or like the records of the same name and type already there.
|
||||
// A new name and type is shared, unless the principal may not change it
|
||||
// in every linked view (an ACME token for the external view only): then
|
||||
// it is local to z's view, which is all they may change.
|
||||
func (x *expander) localFor(z *model.Zone, name, typ string, want *bool) (bool, error) {
|
||||
has := x.st.HasLocal(z.ID, name, typ)
|
||||
if want == nil {
|
||||
return has, nil
|
||||
if want != nil {
|
||||
return *want, nil
|
||||
}
|
||||
return *want, nil
|
||||
if x.st.HasLocal(z.ID, name, typ) {
|
||||
return true, nil
|
||||
}
|
||||
if len(x.st.SharedRecords(z.ID, name, typ)) > 0 {
|
||||
return false, nil
|
||||
}
|
||||
var not []string
|
||||
for _, o := range x.st.LinkedWith(z) {
|
||||
if !x.p.Can(recordAction(x.st, o, name, typ, true)) {
|
||||
not = append(not, x.st.ViewName(o))
|
||||
}
|
||||
}
|
||||
if len(not) > 0 {
|
||||
x.warnings = append(x.warnings, fmt.Sprintf("%s %s is local to view %s: you may not change it in %s", dnsname.Display(name), typ, x.st.ViewName(z), strings.Join(not, ", ")))
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// shareAllOps returns the operations that start a link with zone z: it
|
||||
|
||||
@@ -413,3 +413,37 @@ func TestDeleteLocalOverride(t *testing.T) {
|
||||
t.Errorf("validation: %+v", probs)
|
||||
}
|
||||
}
|
||||
|
||||
// An ACME token for one view of a linked zone: its challenge records are
|
||||
// local to that view, the zone is found without naming the view, and they
|
||||
// go again without touching the other view.
|
||||
func TestLinkedACMEToken(t *testing.T) {
|
||||
env := linkedEnv(t)
|
||||
env.submit(env.admin, NewRequest(OpZoneCreate, 0, ZoneInput{Name: model.Ptr("example.com"), View: model.Ptr("external"), LinkWith: "internal"}))
|
||||
u := &model.User{Username: "certbot", Grants: []model.Grant{{Resource: auth.ResRecords, Access: "write", Zones: []string{"example.com"}, Views: []string{"external"}, Names: []string{"_acme-challenge.*"}, Types: []string{"TXT"}}}}
|
||||
if err := env.st.CreateUser(env.ctx, u); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
bot := &auth.Principal{User: u, Method: "token"}
|
||||
name, typ, data := "_acme-challenge", "TXT", "token-value"
|
||||
res := env.submit(bot, NewRequest(OpRecordCreate, 0, RecordInput{Zone: "example.com", Name: &name, Type: &typ, Data: &data}))
|
||||
if !slices.Contains(env.view("external"), "_acme-challenge TXT \"token-value\" L") || slices.ContainsFunc(env.view("internal"), func(s string) bool { return strings.HasPrefix(s, "_acme-challenge") }) {
|
||||
t.Errorf("challenge: internal %q external %q", env.view("internal"), env.view("external"))
|
||||
}
|
||||
if !slices.ContainsFunc(res.Warnings, func(w string) bool { return strings.Contains(w, "local to view external") }) {
|
||||
t.Errorf("warnings %q", res.Warnings)
|
||||
}
|
||||
env.submit(bot, NewRequest(OpRecordDelete, 0, RecordInput{Zone: "example.com", Name: &name, Type: &typ, Data: &data}))
|
||||
if slices.ContainsFunc(env.view("external"), func(s string) bool { return strings.HasPrefix(s, "_acme-challenge") }) {
|
||||
t.Errorf("not deleted: %q", env.view("external"))
|
||||
}
|
||||
// The admin, who may change both, still gets a shared record.
|
||||
env.submit(env.admin, NewRequest(OpRecordCreate, 0, RecordInput{Zone: "example.com", View: "internal", Name: model.Ptr("both"), Type: model.Ptr("A"), Data: model.Ptr("192.0.2.7")}))
|
||||
if !slices.Contains(env.view("external"), "both A 192.0.2.7 S") {
|
||||
t.Errorf("admin: %q", env.view("external"))
|
||||
}
|
||||
// Without a view, the admin is still asked which.
|
||||
if e := env.submitErr(env.admin, NewRequest(OpRecordCreate, 0, RecordInput{Zone: "example.com", Name: model.Ptr("x"), Type: model.Ptr("A"), Data: model.Ptr("192.0.2.8")})); !strings.Contains(e.Message, "several views (internal, external)") {
|
||||
t.Errorf("admin without a view: %v", e)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user