forked from gravitational/teleport
/
errors.go
248 lines (208 loc) · 7.71 KB
/
errors.go
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
Copyright 2021 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"fmt"
"net/http"
"strings"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/go-mysql-org/go-mysql/mysql"
"github.com/gravitational/trace"
"github.com/gravitational/trace/trail"
"github.com/jackc/pgconn"
"github.com/jackc/pgerrcode"
"google.golang.org/api/googleapi"
"google.golang.org/grpc/status"
"github.com/zmb3/teleport/api/types"
awslib "github.com/zmb3/teleport/lib/cloud/aws"
azurelib "github.com/zmb3/teleport/lib/cloud/azure"
"github.com/zmb3/teleport/lib/defaults"
dbiam "github.com/zmb3/teleport/lib/srv/db/common/iam"
)
// ConvertError converts errors to trace errors.
func ConvertError(err error) error {
if err == nil {
return nil
}
// Unwrap original error first.
if _, ok := err.(*trace.TraceErr); ok {
return ConvertError(trace.Unwrap(err))
}
if pgErr, ok := err.(pgError); ok {
return ConvertError(pgErr.Unwrap())
}
if causer, ok := err.(causer); ok {
return ConvertError(causer.Cause())
}
if _, ok := status.FromError(err); ok {
return trail.FromGRPC(err)
}
switch e := trace.Unwrap(err).(type) {
case *googleapi.Error:
return convertGCPError(e)
case awserr.RequestFailure:
return awslib.ConvertRequestFailureError(e)
case *azcore.ResponseError:
return azurelib.ConvertResponseError(e)
case *pgconn.PgError:
return convertPostgresError(e)
case *mysql.MyError:
return convertMySQLError(e)
}
return err // Return unmodified.
}
// convertGCPError converts GCP errors to trace errors.
func convertGCPError(err *googleapi.Error) error {
switch err.Code {
case http.StatusForbidden:
return trace.AccessDenied(err.Error())
case http.StatusConflict:
return trace.CompareFailed(err.Error())
}
return err // Return unmodified.
}
// convertPostgresError converts Postgres driver errors to trace errors.
func convertPostgresError(err *pgconn.PgError) error {
switch err.Code {
case pgerrcode.InvalidAuthorizationSpecification, pgerrcode.InvalidPassword:
return trace.AccessDenied(err.Error())
}
return err // Return unmodified.
}
// convertMySQLError converts MySQL driver errors to trace errors.
func convertMySQLError(err *mysql.MyError) error {
switch err.Code {
case mysql.ER_ACCESS_DENIED_ERROR:
return trace.AccessDenied(err.Error())
}
return err // Return unmodified.
}
// causer defines an interface for errors wrapped by the "errors" package.
type causer interface {
Cause() error
}
// pgError defines an interface for errors wrapped by Postgres driver.
type pgError interface {
Unwrap() error
}
// ConvertConnectError converts common connection errors to trace errors with
// extra information/recommendations if necessary.
func ConvertConnectError(err error, sessionCtx *Session) error {
if err == nil {
return nil
}
errString := err.Error()
switch {
case strings.Contains(errString, "x509: certificate signed by unknown authority"):
return trace.AccessDenied("Database service cannot validate database's certificate: %v. Please verify if the correct CA bundle is used in the database config.", err)
case strings.Contains(errString, "x509: certificate has expired or is not yet valid"):
return trace.ConnectionProblem(
err,
"Connection Failure. Database service could not validate database’s certificate: certificate expired or is not yet valid. "+
"More info at: https://goteleport.com/docs/database-access/troubleshooting#certificate-expired-or-is-not-yet-valid",
)
case strings.Contains(errString, "tls: unknown certificate authority"):
return trace.AccessDenied("Database cannot validate client certificate generated by database service: %v.", err)
}
err = ConvertError(err)
if trace.IsAccessDenied(err) {
switch sessionCtx.Database.GetType() {
case types.DatabaseTypeRDS:
return createRDSAccessDeniedError(err, sessionCtx)
case types.DatabaseTypeRDSProxy:
return createRDSProxyAccessDeniedError(err, sessionCtx)
case types.DatabaseTypeAzure:
return createAzureAccessDeniedError(err, sessionCtx)
}
}
return trace.Wrap(err)
}
// createRDSAccessDeniedError creates an error with help message to setup IAM
// auth for RDS.
func createRDSAccessDeniedError(err error, sessionCtx *Session) error {
policy, getPolicyErr := dbiam.GetReadableAWSPolicyDocument(sessionCtx.Database)
if getPolicyErr != nil {
policy = fmt.Sprintf("failed to generate IAM policy: %v", getPolicyErr)
}
switch sessionCtx.Database.GetProtocol() {
case defaults.ProtocolMySQL:
return trace.AccessDenied(`Could not connect to database:
%v
Make sure that IAM auth is enabled for MySQL user %q and Teleport database
agent's IAM policy has "rds-connect" permissions (note that IAM changes may
take a few minutes to propagate):
%v
`, err, sessionCtx.DatabaseUser, policy)
case defaults.ProtocolPostgres:
return trace.AccessDenied(`Could not connect to database:
%v
Make sure that Postgres user %q has "rds_iam" role and Teleport database
agent's IAM policy has "rds-connect" permissions (note that IAM changes may
take a few minutes to propagate):
%v
`, err, sessionCtx.DatabaseUser, policy)
default:
return trace.Wrap(err)
}
}
// createRDSProxyAccessDeniedError creates an error with help message to setup
// IAM auth for RDS Proxy.
func createRDSProxyAccessDeniedError(err error, sessionCtx *Session) error {
policy, getPolicyErr := dbiam.GetReadableAWSPolicyDocument(sessionCtx.Database)
if getPolicyErr != nil {
policy = fmt.Sprintf("failed to generate IAM policy: %v", getPolicyErr)
}
return trace.AccessDenied(`Could not connect to database:
%v
Make sure credentials for %v user %q is available in one of the Secrets Manager
secrets associated with the RDS Proxy and the IAM Authentication is set to
"required" for that secret.
Also, make sure the Teleport database agent's IAM policy has "rds-connect"
permissions (note that IAM changes may take a few minutes to propagate):
%v
`,
err,
defaults.ReadableDatabaseProtocol(sessionCtx.Database.GetProtocol()),
sessionCtx.DatabaseUser,
policy,
)
}
// createAzureAccessDeniedError creates an error with help message to setup AAD
// auth for PostgreSQL/MySQL.
func createAzureAccessDeniedError(err error, sessionCtx *Session) error {
switch sessionCtx.Database.GetProtocol() {
case defaults.ProtocolMySQL:
return trace.AccessDenied(`Could not connect to database:
%v
Make sure that Azure Active Directory auth is configured for MySQL user %q and the Teleport database
agent's service principal. See: https://goteleport.com/docs/database-access/guides/azure-postgres-mysql/
`, err, sessionCtx.DatabaseUser)
case defaults.ProtocolPostgres:
return trace.AccessDenied(`Could not connect to database:
%v
Make sure that Azure Active Directory auth is configured for Postgres user %q and the Teleport database
agent's service principal. See: https://goteleport.com/docs/database-access/guides/azure-postgres-mysql/
`, err, sessionCtx.DatabaseUser)
default:
return trace.Wrap(err)
}
}
// IsUnrecognizedAWSEngineNameError checks if the err is non-nil and came from using an engine filter that the
// AWS region does not recognize.
func IsUnrecognizedAWSEngineNameError(err error) bool {
if err == nil {
return false
}
return strings.Contains(strings.ToLower(err.Error()), "unrecognized engine name")
}