1// Copyright (c) 2017 Uber Technologies, Inc. 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 15package baggage 16 17const ( 18 defaultMaxValueLength = 2048 19) 20 21// Restriction determines whether a baggage key is allowed and contains any restrictions on the baggage value. 22type Restriction struct { 23 keyAllowed bool 24 maxValueLength int 25} 26 27// NewRestriction returns a new Restriction. 28func NewRestriction(keyAllowed bool, maxValueLength int) *Restriction { 29 return &Restriction{ 30 keyAllowed: keyAllowed, 31 maxValueLength: maxValueLength, 32 } 33} 34 35// KeyAllowed returns whether the baggage key for this restriction is allowed. 36func (r *Restriction) KeyAllowed() bool { 37 return r.keyAllowed 38} 39 40// MaxValueLength returns the max length for the baggage value. 41func (r *Restriction) MaxValueLength() int { 42 return r.maxValueLength 43} 44 45// RestrictionManager keeps track of valid baggage keys and their restrictions. The manager 46// will return a Restriction for a specific baggage key which will determine whether the baggage 47// key is allowed for the current service and any other applicable restrictions on the baggage 48// value. 49type RestrictionManager interface { 50 GetRestriction(service, key string) *Restriction 51} 52 53// DefaultRestrictionManager allows any baggage key. 54type DefaultRestrictionManager struct { 55 defaultRestriction *Restriction 56} 57 58// NewDefaultRestrictionManager returns a DefaultRestrictionManager. 59func NewDefaultRestrictionManager(maxValueLength int) *DefaultRestrictionManager { 60 if maxValueLength == 0 { 61 maxValueLength = defaultMaxValueLength 62 } 63 return &DefaultRestrictionManager{ 64 defaultRestriction: &Restriction{keyAllowed: true, maxValueLength: maxValueLength}, 65 } 66} 67 68// GetRestriction implements RestrictionManager#GetRestriction. 69func (m *DefaultRestrictionManager) GetRestriction(service, key string) *Restriction { 70 return m.defaultRestriction 71} 72