1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
package tvmauth_test
import (
"context"
"fmt"
"github.com/ydb-platform/ydb/library/go/core/log/nop"
"github.com/ydb-platform/ydb/library/go/yandex/tvm"
"github.com/ydb-platform/ydb/library/go/yandex/tvm/tvmauth"
)
func ExampleNewAPIClient_getServiceTicketsWithAliases() {
blackboxAlias := "blackbox"
datasyncAlias := "datasync"
settings := tvmauth.TvmAPISettings{
SelfID: 1000501,
ServiceTicketOptions: tvmauth.NewAliasesOptions(
"bAicxJVa5uVY7MjDlapthw",
map[string]tvm.ClientID{
blackboxAlias: 1000502,
datasyncAlias: 1000503,
}),
DiskCacheDir: "/var/tmp/cache/tvm/",
}
c, err := tvmauth.NewAPIClient(settings, &nop.Logger{})
if err != nil {
panic(err)
}
// ...
serviceTicket, _ := c.GetServiceTicketForAlias(context.Background(), blackboxAlias)
fmt.Printf("Service ticket for visiting backend: %s", serviceTicket)
}
func ExampleNewAPIClient_getServiceTicketsWithID() {
blackboxID := tvm.ClientID(1000502)
datasyncID := tvm.ClientID(1000503)
settings := tvmauth.TvmAPISettings{
SelfID: 1000501,
ServiceTicketOptions: tvmauth.NewIDsOptions(
"bAicxJVa5uVY7MjDlapthw",
[]tvm.ClientID{
blackboxID,
datasyncID,
}),
DiskCacheDir: "/var/tmp/cache/tvm/",
}
c, err := tvmauth.NewAPIClient(settings, &nop.Logger{})
if err != nil {
panic(err)
}
// ...
serviceTicket, _ := c.GetServiceTicketForID(context.Background(), blackboxID)
fmt.Printf("Service ticket for visiting backend: %s", serviceTicket)
}
func ExampleNewAPIClient_checkServiceTicket() {
// allowed tvm consumers for your service
acl := map[tvm.ClientID]interface{}{}
settings := tvmauth.TvmAPISettings{
SelfID: 1000501,
EnableServiceTicketChecking: true,
DiskCacheDir: "/var/tmp/cache/tvm/",
}
c, err := tvmauth.NewAPIClient(settings, &nop.Logger{})
if err != nil {
panic(err)
}
// ...
serviceTicketFromRequest := "kek"
serviceTicketStruct, err := c.CheckServiceTicket(context.Background(), serviceTicketFromRequest)
if err != nil {
response := map[string]string{
"error": "service ticket is invalid",
"desc": err.Error(),
"status": err.(*tvm.TicketError).Status.String(),
}
if serviceTicketStruct != nil {
response["debug_info"] = serviceTicketStruct.DbgInfo
}
panic(response) // return 403
}
if _, ok := acl[serviceTicketStruct.SrcID]; !ok {
response := map[string]string{
"error": fmt.Sprintf("tvm client id is not allowed: %d", serviceTicketStruct.SrcID),
}
panic(response) // return 403
}
// proceed...
}
func ExampleNewAPIClient_checkUserTicket() {
env := tvm.BlackboxTest
settings := tvmauth.TvmAPISettings{
SelfID: 1000501,
EnableServiceTicketChecking: true,
BlackboxEnv: &env,
DiskCacheDir: "/var/tmp/cache/tvm/",
}
c, err := tvmauth.NewAPIClient(settings, &nop.Logger{})
if err != nil {
panic(err)
}
// ...
serviceTicketFromRequest := "kek"
userTicketFromRequest := "lol"
_, _ = c.CheckServiceTicket(context.Background(), serviceTicketFromRequest) // See example for this method
userTicketStruct, err := c.CheckUserTicket(context.Background(), userTicketFromRequest)
if err != nil {
response := map[string]string{
"error": "user ticket is invalid",
"desc": err.Error(),
"status": err.(*tvm.TicketError).Status.String(),
}
if userTicketStruct != nil {
response["debug_info"] = userTicketStruct.DbgInfo
}
panic(response) // return 403
}
fmt.Printf("Got user in request: %d", userTicketStruct.DefaultUID)
// proceed...
}
func ExampleNewAPIClient_createClientWithAllSettings() {
blackboxAlias := "blackbox"
datasyncAlias := "datasync"
env := tvm.BlackboxTest
settings := tvmauth.TvmAPISettings{
SelfID: 1000501,
ServiceTicketOptions: tvmauth.NewAliasesOptions(
"bAicxJVa5uVY7MjDlapthw",
map[string]tvm.ClientID{
blackboxAlias: 1000502,
datasyncAlias: 1000503,
}),
EnableServiceTicketChecking: true,
BlackboxEnv: &env,
DiskCacheDir: "/var/tmp/cache/tvm/",
}
_, _ = tvmauth.NewAPIClient(settings, &nop.Logger{})
}
func ExampleNewToolClient_getServiceTicketsWithAliases() {
// should be configured in tvmtool
blackboxAlias := "blackbox"
settings := tvmauth.TvmToolSettings{
Alias: "my_service",
Port: 18000,
AuthToken: "kek",
}
c, err := tvmauth.NewToolClient(settings, &nop.Logger{})
if err != nil {
panic(err)
}
// ...
serviceTicket, _ := c.GetServiceTicketForAlias(context.Background(), blackboxAlias)
fmt.Printf("Service ticket for visiting backend: %s", serviceTicket)
// please extrapolate other methods for this way of construction
}
|