go.temporal.io/server/common/headers/headers.go

167 LOC · 73 covered · 94 uncovered · 30 ranges · 3527 concepts · 23 introducers · 1672 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 package headers
2
3 import (
4 "context"
5 "strings"
6
7 commonpb "go.temporal.io/api/common/v1"
8 "google.golang.org/grpc/metadata"
9 )
10
11 // Note the nexusoperations component references these headers and adds them to a list of disallowed headers for users to set.
12 // If any other headers are added for internal use, they should be added to the disallowed headers list.
13 const (
14 ClientNameHeaderName = "client-name"
15 ClientVersionHeaderName = "client-version"
16 SupportedServerVersionsHeaderName = "supported-server-versions"
17 SupportedFeaturesHeaderName = "supported-features"
18 SupportedFeaturesHeaderDelim = ","
19
20 CallerNameHeaderName = "caller-name"
21 CallerTypeHeaderName = "caller-type"
22 CallOriginHeaderName = "call-initiation"
23
24 PrincipalTypeHeaderName = "temporal-principal-type"
25 PrincipalNameHeaderName = "temporal-principal-name"
26
27 ExperimentHeaderName = "temporal-experiment"
28 )
29
30 var (
31 // propagateHeaders are the headers to propagate from the frontend to other services.
32 propagateHeaders = []string{
33 ClientNameHeaderName,
34 ClientVersionHeaderName,
35 SupportedServerVersionsHeaderName,
36 SupportedFeaturesHeaderName,
37 CallerNameHeaderName,
38 CallerTypeHeaderName,
39 CallOriginHeaderName,
40 PrincipalTypeHeaderName,
41 PrincipalNameHeaderName,
42 }
43 )
44
45 // GetValues returns header values for passed header names.
46 // It always returns slice of the same size as number of passed header names.
47 > func GetValues(ctx context.Context, headerNames ...string) []string { headers.go ×2
48 > headerValues := make([]string, len(headerNames))
49 >
50 > for i, headerName := range headerNames {
51 > if values := metadata.ValueFromIncomingContext(ctx, headerName); len(values) > 0 {
52 > headerValues[i] = values[0] headers.go ×1
53 > }
54 }
55
56 > return headerValues headers.go ×2
57 }
58
59 // Propagate propagates version headers from incoming context to outgoing context.
60 // It copies all headers to outgoing context only if they are exist in incoming context
61 // and doesn't exist in outgoing context already.
62 > func Propagate(ctx context.Context) context.Context { headers.go ×4
63 > headersToAppend := make([]string, 0, len(propagateHeaders)*2)
64 > mdOutgoing, mdOutgoingExist := metadata.FromOutgoingContext(ctx)
65 > for _, headerName := range propagateHeaders {
66 > if incomingValue := metadata.ValueFromIncomingContext(ctx, headerName); len(incomingValue) > 0 && len(mdOutgoing.Get(headerName)) == 0 {
67 > headersToAppend = append(headersToAppend, headerName, incomingValue[0]) headers.go ×1
68 > }
69 }
70 > if headersToAppend != nil { headers.go ×4
71 > if mdOutgoingExist {
72 > ctx = metadata.AppendToOutgoingContext(ctx, headersToAppend...) headers.go ×1
73 > } else { headers.go ×4
74 > ctx = metadata.NewOutgoingContext(ctx, metadata.Pairs(headersToAppend...)) headers.go ×1
75 > }
76 }
77 > return ctx headers.go ×4
78 }
79
80 // HeaderGetter is an interface for getting a single header value from a case insensitive key.
81 type HeaderGetter interface {
82 Get(string) string
83 }
84
85 // Wrapper for gRPC metadata that exposes a helper to extract a single metadata value.
86 type GRPCHeaderGetter struct {
87 ctx context.Context
88 }
89
90 > func NewGRPCHeaderGetter(ctx context.Context) GRPCHeaderGetter { headers.go ×1
91 > return GRPCHeaderGetter{ctx: ctx}
92 > }
93
94 // Get a single value from the underlying gRPC metadata.
95 // Returns an empty string if the metadata key is unset.
96 > func (h GRPCHeaderGetter) Get(key string) string { headers.go ×1
97 > if values := metadata.ValueFromIncomingContext(h.ctx, key); len(values) > 0 {
98 > return values[0] headers.go ×1
99 > }
100 > return "" headers.go ×1
101 }
102
103 // IsExperimentRequested checks if a specific experiment is present in the temporal-experiment header.
104 // Returns true if the experiment is explicitly listed or if "*" (wildcard) is present.
105 // Headers exceeding a length of 100 will be skipped.
106 > func IsExperimentRequested(ctx context.Context, experiment string) bool { headers.go ×1
107 > experimentalValues := metadata.ValueFromIncomingContext(ctx, ExperimentHeaderName)
108 >
109 > for _, headerValue := range experimentalValues {
110 > // limit value size to prevent misuse headers.go ×1
111 > if len(headerValue) > 100 {
112 > continue headers.go ×1
113 }
114 > for requested := range strings.SplitSeq(headerValue, ",") { headers.go ×1
115 > requested = strings.TrimSpace(requested)
116 > if requested == "*" || requested == experiment {
117 > return true
118 > }
119 }
120 }
121
122 > return false headers.go ×1
123 }
124
125 // StripPrincipal removes principal headers from incoming metadata to prevent
126 // external callers from spoofing principal identity.
127 > func StripPrincipal(ctx context.Context) context.Context { headers.go ×1
128 > mdIncoming, ok := metadata.FromIncomingContext(ctx)
129 > if !ok {
130 > return ctx headers.go ×1
131 > }
132 > mdIncoming.Delete(PrincipalTypeHeaderName) headers.go ×1
133 > mdIncoming.Delete(PrincipalNameHeaderName)
134 > return metadata.NewIncomingContext(ctx, mdIncoming)
135 }
136
137 // SetPrincipal sets the principal type and name headers in the incoming metadata.
138 > func SetPrincipal(ctx context.Context, principal *commonpb.Principal) context.Context { headers.go ×2
139 > return setIncomingMD(ctx, map[string]string{
140 > PrincipalTypeHeaderName: principal.GetType(),
141 > PrincipalNameHeaderName: principal.GetName(),
142 > })
143 > }
144
145 // GetPrincipal retrieves the principal from the context headers. Returns nil if principal is not set.
146 > func GetPrincipal(ctx context.Context) *commonpb.Principal { headers.go ×1
147 > values := GetValues(ctx, PrincipalTypeHeaderName, PrincipalNameHeaderName)
148 > if values[0] == "" && values[1] == "" {
149 > return nil headers.go ×1
150 > }
151 > return &commonpb.Principal{Type: values[0], Name: values[1]} headers.go ×2
152 }
153
154 // setIncomingMD sets the key-value pairs in the incoming metadata.
155 // Empty values are ignored.
156 > func setIncomingMD(ctx context.Context, kv map[string]string) context.Context { headers.go ×3
157 > mdIncoming, ok := metadata.FromIncomingContext(ctx)
158 > if !ok {
159 > mdIncoming = metadata.MD{} headers.go ×1
160 > }
161 > for k, v := range kv { headers.go ×3
162 > if v != "" {
163 > mdIncoming.Set(k, v)
164 > }
165 }
166 > return metadata.NewIncomingContext(ctx, mdIncoming) headers.go ×3
167 }