Files
teleport/lib/msgraph/errors.go
T
Justinas Stankevičius c5a5f8e7a2 Replace MS Graph SDK with an in-house client (#44069)
* Add MS Graph client skeleton

* Complete retry logic

* Add GetID() to GroupMember

* Parse MS Graph API error responses

* Pull out page size into config

* Add missing "patch" methods to client interface

* Move code around

* Factor out roundtrip()

* Fill out remaining methods

* Add godocs and solve lints

* Run gci

* Move godocs to the interface

* Add missing method

* Finish errors_test

* close body in test

* nit

* Add license headers

* Move azureoidc onboarding code to msgraph.Client

* Make AzureTokenProvider public

* Use cmp.Or to shorten config defaults handling

* Address review nits

* Parallelize tests

* nit

* Respect context cancellation in retry logic

* Utilize BlockUntil in tests

* Remove Client interface, make client struct public

* Prefer defaults.HTTPClient to http.DefaultClient

* Iterate entra apps, do not buffer all in memory

* nit

* Set additional timeouts on default HTTP client

* Escape path segments when constructing URLs

* Add a general timeout for default HTTP client
2024-07-17 15:51:39 +00:00

73 lines
2.0 KiB
Go

// Teleport
// Copyright (C) 2024 Gravitational, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package msgraph
import (
"encoding/json"
"fmt"
"io"
"strings"
"github.com/gravitational/trace"
)
// unsupportedGroupMember is an internal error to indicate that
// the `groupmembers` endpoint has returned a member of type that we do not support (yet).
type unsupportedGroupMember struct {
Type string
}
func (u *unsupportedGroupMember) Error() string {
return fmt.Sprintf("Unsupported group member: %q", u.Type)
}
type graphErrorResponse struct {
Error *GraphError `json:"error,omitempty"`
}
// GraphError defines the structure of errors returned from MS Graph API.
type GraphError struct {
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
InnerError *GraphError `json:"innerError,omitempty"`
Details []GraphError `json:"details,omitempty"`
}
func (g *GraphError) Error() string {
var parts []string
if g.Code != "" {
parts = append(parts, strings.TrimPrefix(g.Code, "Request_"))
}
if g.Message != "" {
parts = append(parts, g.Message)
}
return strings.Join(parts, ": ")
}
func readError(r io.Reader) (*GraphError, error) {
var errResponse graphErrorResponse
if err := json.NewDecoder(r).Decode(&errResponse); err != nil {
return nil, trace.Wrap(err)
}
if errResponse.Error != nil {
return errResponse.Error, nil
}
return nil, nil
}