Example usage for org.springframework.vault.client VaultResponses getError

List of usage examples for org.springframework.vault.client VaultResponses getError

Introduction

In this page you can find the example usage for org.springframework.vault.client VaultResponses getError.

Prototype

@SuppressWarnings("unchecked")
public static String getError(String json) 

Source Link

Document

Obtain the error message from a JSON response.

Usage

From source file:org.springframework.vault.authentication.AuthenticationStepsExecutor.java

@Override
@SuppressWarnings("unchecked")
public VaultToken login() throws VaultException {

    Object state = null;//from   w w w .  j av  a2s.com

    for (Node<?> o : chain.steps) {

        if (logger.isDebugEnabled()) {
            logger.debug(String.format("Executing %s with current state %s", o, state));
        }

        try {
            if (o instanceof HttpRequestNode) {
                state = doHttpRequest((HttpRequestNode<Object>) o, state);
            }

            if (o instanceof AuthenticationSteps.MapStep) {
                state = doMapStep((MapStep<Object, Object>) o, state);
            }

            if (o instanceof OnNextStep) {
                state = doOnNext((OnNextStep<Object>) o, state);
            }

            if (o instanceof AuthenticationSteps.SupplierStep<?>) {
                state = doSupplierStep((SupplierStep<Object>) o);
            }

            if (logger.isDebugEnabled()) {
                logger.debug(String.format("Executed %s with current state %s", o, state));
            }
        } catch (HttpStatusCodeException e) {
            throw new VaultException(
                    String.format("HTTP request %s in state %s failed with Status %s and body %s", o, state,
                            e.getStatusCode(), VaultResponses.getError(e.getResponseBodyAsString())));
        } catch (RuntimeException e) {
            throw new VaultException(String.format("Authentication execution failed in %s", o), e);
        }
    }

    if (state instanceof VaultToken) {
        return (VaultToken) state;
    }

    if (state instanceof VaultResponse) {

        VaultResponse response = (VaultResponse) state;
        Assert.state(response.getAuth() != null, "Auth field must not be null");
        return LoginTokenUtil.from(response.getAuth());
    }

    throw new IllegalStateException(
            String.format("Cannot retrieve VaultToken from authentication chain. Got instead %s", state));
}

From source file:org.springframework.vault.authentication.AwsIamAuthentication.java

@SuppressWarnings("unchecked")
private VaultToken createTokenUsingAwsIam() {

    Map<String, String> login = new HashMap<>();

    login.put("iam_http_request_method", "POST");
    login.put("iam_request_url", Base64Utils.encodeToString(options.getEndpointUri().toString().getBytes()));
    login.put("iam_request_body", REQUEST_BODY_BASE64_ENCODED);

    String headerJson = getSignedHeaders(options);

    login.put("iam_request_headers", Base64Utils.encodeToString(headerJson.getBytes()));

    if (!StringUtils.isEmpty(options.getRole())) {
        login.put("role", options.getRole());
    }/*ww w.jav a  2 s  .c  o  m*/

    try {

        VaultResponse response = this.vaultRestOperations.postForObject("auth/{mount}/login", login,
                VaultResponse.class, options.getPath());

        Assert.state(response != null && response.getAuth() != null, "Auth field must not be null");

        if (logger.isDebugEnabled()) {

            if (response.getAuth().get("metadata") instanceof Map) {
                Map<Object, Object> metadata = (Map<Object, Object>) response.getAuth().get("metadata");
                logger.debug(
                        String.format("Login successful using AWS-IAM authentication for user id %s, ARN %s",
                                metadata.get("client_user_id"), metadata.get("canonical_arn")));
            } else {
                logger.debug("Login successful using AWS-IAM authentication");
            }
        }

        return LoginTokenUtil.from(response.getAuth());
    } catch (HttpStatusCodeException e) {
        throw new VaultException(String.format("Cannot login using AWS-IAM: %s",
                VaultResponses.getError(e.getResponseBodyAsString())));
    }
}