Exemple #1
0
func (s *ErrorsSuite) TestErrorIsType(c *gc.C) {
	rootCause := errors.NewBadRequestf(nil, "some value", "")
	// Construct a new error, based on a bad request root cause.
	err := errors.Newf(rootCause, "an error occurred")
	// Check that the error is not falsely identified as something it is not.
	c.Assert(errors.IsNotAuthorized(err), gc.Equals, false)
	// Check that the error is correctly identified as a not found error.
	c.Assert(errors.IsBadRequest(err), gc.Equals, true)
}
Exemple #2
0
func (s *ErrorsSuite) TestCreateNotAuthorizedError(c *gc.C) {
	context := "context"
	err := errors.NewNotAuthorizedf(nil, context, "It was not authorized: %s", context)
	c.Assert(errors.IsNotAuthorized(err), gc.Equals, true)
	c.Assert(err.Error(), gc.Equals, "It was not authorized: context")
}
Exemple #3
0
// verifyCredentials issues a cheap, non-modifying request to Joyent to
// verify the configured credentials. If verification fails, a user-friendly
// error will be returned, and the original error will be logged at debug
// level.
var verifyCredentials = func(e *joyentEnviron) error {
	creds, err := credentials(e.Ecfg())
	if err != nil {
		return err
	}
	httpClient := client.NewClient(e.Ecfg().sdcUrl(), cloudapi.DefaultAPIVersion, creds, nil)
	apiClient := cloudapi.New(httpClient)
	_, err = apiClient.CountMachines()
	if err != nil {
		logger.Debugf("joyent request failed: %v", err)
		if joyenterrors.IsInvalidCredentials(err) || joyenterrors.IsNotAuthorized(err) {
			return errors.New("authentication failed.\n" + unauthorisedMessage)
		}
		return err
	}
	return nil
}

func credentials(cfg *environConfig) (*auth.Credentials, error) {
	authentication, err := auth.NewAuth(cfg.mantaUser(), cfg.privateKey(), cfg.algorithm())
	if err != nil {
		return nil, errors.Errorf("cannot create credentials: %v", err)
	}
	return &auth.Credentials{
		UserAuthentication: authentication,
		MantaKeyId:         cfg.mantaKeyId(),