@Override public List<SecretValue> getValues(List<SecretReference> refs, Host host) throws IOException { Map<Long, Secret> secrets = secretDao.getSecrets(refs); Map<Long, String> values = getValues(secrets.values(), host); List<SecretValue> result = new ArrayList<>(); for (SecretReference ref : refs) { Secret secret = secrets.get(ref.getSecretId()); if (secret == null) { continue; } SecretValue value = new SecretValue(ref, secret, values.get(secret.getId())); result.add(value); } return result; }
@Override public HandlerResult handle(ProcessState state, ProcessInstance process) { Secret secret = (Secret)state.getResource(); try { secretsService.delete(secret.getAccountId(), secret.getValue()); } catch (IOException e) { log.error("Failed to delete secret from storage [{}]", secret.getId(), e); throw new IllegalStateException(e); } return null; }
protected Map<Long, String> getValues(Collection<Secret> secrets, Host host) throws IOException { List<Secret> secretsList = new ArrayList<>(secrets); Map<Long, String> result = new HashMap<>(); Map<String, Object> hostInfo = DataAccessor.fieldMap(host, HostConstants.FIELD_INFO); Object rewrapKey = CollectionUtils.getNestedValue(hostInfo, "hostKey", "data"); Map<String, Object> input = new HashMap<>(); input.put("data", toData(secretsList)); input.put("rewrapKey", rewrapKey); Map<String, Object> response = Request.Post(SECRETS_URL.get() + BULK_PATH). bodyString(jsonMapper.writeValueAsString(input), ContentType.APPLICATION_JSON) .execute().handleResponse(new ResponseHandler<Map<String, Object>>() { @Override public Map<String, Object> handleResponse(HttpResponse response) throws ClientProtocolException, IOException { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode >= 300) { throw new IOException("Failed to rewrap secret :" + response.getStatusLine().getReasonPhrase()); } return jsonMapper.readValue(response.getEntity().getContent()); } }); List<?> wrapped = CollectionUtils.toList(response.get("data")); for (int i = 0; i < secretsList.size(); i++) { Secret secret = secretsList.get(i); result.put(secret.getId(), CollectionUtils.toMap(wrapped.get(i)).get("rewrapText").toString()); } return result; }
@Override public Map<Long, Secret> getSecrets(List<SecretReference> refs) { Set<Long> ids = new HashSet<>(); for (SecretReference ref : refs) { ids.add(ref.getSecretId()); } Map<Long, Secret> result = new HashMap<Long, Secret>(); for (Secret secret : create().select(SECRET.fields()) .from(SECRET) .where(SECRET.ID.in(ids)) .fetchInto(SecretRecord.class)) { result.put(secret.getId(), secret); } return result; }
/** * {@inheritDoc} */ @Override public void from(io.cattle.platform.core.model.Secret from) { setId(from.getId()); setName(from.getName()); setAccountId(from.getAccountId()); setKind(from.getKind()); setUuid(from.getUuid()); setDescription(from.getDescription()); setState(from.getState()); setCreated(from.getCreated()); setRemoved(from.getRemoved()); setRemoveTime(from.getRemoveTime()); setData(from.getData()); setValue(from.getValue()); setStackId(from.getStackId()); }