go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/tokenserver/appengine/impl/delegation/config_validation.go (about)

     1  // Copyright 2016 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package delegation
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"go.chromium.org/luci/auth/identity"
    22  	"go.chromium.org/luci/common/data/stringset"
    23  	"go.chromium.org/luci/config/validation"
    24  
    25  	"go.chromium.org/luci/tokenserver/api/admin/v1"
    26  	"go.chromium.org/luci/tokenserver/appengine/impl/utils/policy"
    27  )
    28  
    29  // validateConfigBundle validates the structure of a config bundle fetched by
    30  // fetchConfigs.
    31  func validateConfigBundle(ctx *validation.Context, bundle policy.ConfigBundle) {
    32  	ctx.SetFile(delegationCfg)
    33  	cfg, ok := bundle[delegationCfg].(*admin.DelegationPermissions)
    34  	if ok {
    35  		validateDelegationCfg(ctx, cfg)
    36  	} else {
    37  		ctx.Errorf("unexpectedly wrong proto type %T", cfg)
    38  	}
    39  }
    40  
    41  // validateDelegationCfg checks deserialized delegation.cfg.
    42  func validateDelegationCfg(ctx *validation.Context, cfg *admin.DelegationPermissions) {
    43  	names := stringset.New(0)
    44  	for i, rule := range cfg.Rules {
    45  		if rule.Name != "" {
    46  			if names.Has(rule.Name) {
    47  				ctx.Errorf("two rules with identical name %q", rule.Name)
    48  			}
    49  			names.Add(rule.Name)
    50  		}
    51  		validateRule(ctx, fmt.Sprintf("rule #%d: %q", i+1, rule.Name), rule)
    52  	}
    53  }
    54  
    55  // validateRule checks single DelegationRule proto.
    56  //
    57  // See config.proto, DelegationRule for the description of allowed values.
    58  func validateRule(ctx *validation.Context, title string, r *admin.DelegationRule) {
    59  	ctx.Enter(title)
    60  	defer ctx.Exit()
    61  
    62  	if r.Name == "" {
    63  		ctx.Errorf(`"name" is required`)
    64  	}
    65  
    66  	v := identitySetValidator{
    67  		Field:       "requestor",
    68  		Context:     ctx,
    69  		AllowGroups: true,
    70  	}
    71  	v.validate(r.Requestor)
    72  
    73  	v = identitySetValidator{
    74  		Field:              "allowed_to_impersonate",
    75  		Context:            ctx,
    76  		AllowReservedWords: []string{Requestor, Projects}, // '*' is not allowed here though
    77  		AllowGroups:        true,
    78  	}
    79  	v.validate(r.AllowedToImpersonate)
    80  
    81  	v = identitySetValidator{
    82  		Field:              "allowed_audience",
    83  		Context:            ctx,
    84  		AllowReservedWords: []string{Requestor, "*"},
    85  		AllowGroups:        true,
    86  	}
    87  	v.validate(r.AllowedAudience)
    88  
    89  	v = identitySetValidator{
    90  		Field:              "target_service",
    91  		Context:            ctx,
    92  		AllowReservedWords: []string{"*"},
    93  		AllowIDKinds:       []identity.Kind{identity.Service},
    94  	}
    95  	v.validate(r.TargetService)
    96  
    97  	switch {
    98  	case r.MaxValidityDuration == 0:
    99  		ctx.Errorf(`"max_validity_duration" is required`)
   100  	case r.MaxValidityDuration < 0:
   101  		ctx.Errorf(`"max_validity_duration" must be positive`)
   102  	case r.MaxValidityDuration > 24*3600:
   103  		ctx.Errorf(`"max_validity_duration" must be smaller than 86401`)
   104  	}
   105  }
   106  
   107  type identitySetValidator struct {
   108  	Field              string              // name of the field being validated
   109  	Context            *validation.Context // where to emit errors to
   110  	AllowReservedWords []string            // to allow "*" and "REQUESTOR"
   111  	AllowGroups        bool                // true to allow "group:" entries
   112  	AllowIDKinds       []identity.Kind     // permitted identity kinds, or nil if all
   113  }
   114  
   115  func (v *identitySetValidator) validate(items []string) {
   116  	if len(items) == 0 {
   117  		v.Context.Errorf("%q is required", v.Field)
   118  		return
   119  	}
   120  
   121  	v.Context.Enter("%q", v.Field)
   122  	defer v.Context.Exit()
   123  
   124  loop:
   125  	for _, s := range items {
   126  		// A reserved word?
   127  		for _, r := range v.AllowReservedWords {
   128  			if s == r {
   129  				continue loop
   130  			}
   131  		}
   132  
   133  		// A group reference?
   134  		if strings.HasPrefix(s, "group:") {
   135  			if !v.AllowGroups {
   136  				v.Context.Errorf("group entries are not allowed - %q", s)
   137  			} else {
   138  				if s == "group:" {
   139  					v.Context.Errorf("bad group entry %q", s)
   140  				}
   141  			}
   142  			continue
   143  		}
   144  
   145  		// An identity then.
   146  		id, err := identity.MakeIdentity(s)
   147  		if err != nil {
   148  			v.Context.Error(err)
   149  			continue
   150  		}
   151  
   152  		if v.AllowIDKinds != nil {
   153  			allowed := false
   154  			for _, k := range v.AllowIDKinds {
   155  				if id.Kind() == k {
   156  					allowed = true
   157  					break
   158  				}
   159  			}
   160  			if !allowed {
   161  				v.Context.Errorf("identity of kind %q is not allowed here - %q", id.Kind(), s)
   162  			}
   163  		}
   164  	}
   165  }