/** * {@inheritDoc} */ @Override public void from(io.cattle.platform.core.model.Region 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()); setUrl(from.getUrl()); setPublicValue(from.getPublicValue()); setSecretValue(from.getSecretValue()); setLocal(from.getLocal()); }
/** * {@inheritDoc} */ @Override public <E extends io.cattle.platform.core.model.Region> E into(E into) { into.from(this); return into; }
private static Request setHeaders(Request req, Region region) { String publicKey = region.getPublicValue(); String secretKey = region.getSecretValue(); String encodedKeys = Base64.encodeBase64String(String.format("%s:%s", publicKey, secretKey).getBytes()); String auth = String.format("Basic %s", encodedKeys); req.addHeader("Authorization", auth); req.addHeader("X-ENFORCE-AUTHENTICATION", "true"); req.addHeader("Content-Type", "application/json"); req.addHeader("Accept", "application/json"); return req; }
private boolean regionUpdated(Object oldObj, Region region) { Map<String, Object> old = CollectionUtils.toMap(oldObj); if(old.containsKey("url")) { String oldURL = (String) old.get("url"); if(isChanged(oldURL, region.getUrl())) { return true; } } if(old.containsKey("publicValue")) { String oldPublicValue = (String) old.get("publicValue"); if(isChanged(oldPublicValue, region.getPublicValue())) { return true; } } if(old.containsKey("secretValue")) { String oldSecretValue = (String) old.get("secretValue"); if(isChanged(oldSecretValue, region.getSecretValue())) { return true; } } return false; }
@Override public ExternalAgent handleResponse(HttpResponse response) throws ClientProtocolException, IOException { if (response.getStatusLine().getStatusCode() >= 300) { throw new IOException( String.format("Failed to create external agent with uri [%s] in environment [%s] in region [%s]: response error code %s", params.get(AgentConstants.FIELD_URI), targetEnvName, targetRegion.getName(), response.getStatusLine().getReasonPhrase())); } return jsonMapper.readValue(response.getEntity().getContent(), ExternalAgent.class); } });
@Override public boolean isRegionsEmpty(Agent agent, Account account, Map<String, Long> externalLinks, Map<String, ExternalProject> projects, Map<Long, Region> regionsIds, Map<String, Region> regionNameToRegion) { List<Region> regions = objectManager.find(Region.class, REGION.REMOVED, (Object) null); for(Region region : regions) { regionsIds.put(region.getId(), region); regionNameToRegion.put(region.getName(), region); } getLinkedEnvironments(account.getId(), externalLinks, regionsIds, projects); boolean noExternalLinks = externalLinks.isEmpty(); List<? extends ExternalCredential> existing = DataAccessor.fieldObjectList(agent, AccountConstants.FIELD_EXTERNAL_CREDENTIALS, ExternalCredential.class, jsonMapper); boolean noExternalCreds = existing.isEmpty(); if (noExternalLinks && noExternalCreds) { return true; } return false; }
@Override public void doWithLockNoResult() { List<Region> regions = objectManager.find(Region.class, REGION.REMOVED, new Condition(ConditionType.NULL)); if (regions.size() == 0) { return; } Account localAccount = objectManager.loadResource(Account.class, accountId); if (INVALID_STATES.contains(localAccount.getState())) { return; } Map<String, Region> regionsMap = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); Region localRegion = null; for (Region region : regions) { regionsMap.put(region.getName(), region); if (region.getLocal()) { localRegion = region; } } List<AccountLink> toRemove = new ArrayList<>(); List<AccountLink> toUpdate = new ArrayList<>(); Set<String> toCreate = new HashSet<>(); fetchAccountLinks(accountId, regionsMap, localRegion, toRemove, toUpdate, toCreate); reconcileAccountLinks(accountId, regionsMap, toRemove, toUpdate, toCreate); } });
@Override public void run() { List<Region> regions = objectManager.find(Region.class, REGION.REMOVED, new Condition(ConditionType.NULL)); if (regions.size() == 0) { return; } Region localRegion = null; Map<Long, Region> regionMap = new HashMap<Long, Region>(); for(Region region : regions) { regionMap.put(region.getId(), region); if(region.getLocal()) { localRegion = region; } } HashSet<String> existingLinks = new HashSet<String>(); cleanLinks(regionMap, existingLinks, localRegion); cleanAgents(existingLinks); reconcileAccountLinks(); }
Region region = regionsMap.get(regionName); ExternalProject externalProject = null; if (region != null && !INVALID_STATES.contains(region.getState())) { try { String externalProjectKey = String.format("%s:%s", regionName, envName); ACCOUNT_LINK.LINKED_REGION_ID, region.getId(), "linkedAccountUuid", externalProject.getUuid()); toUpdate.add(link); } catch(Exception ex) {
private static String getUrl(Region region) { return region.getUrl(); }
@Override public boolean reconcileAgentExternalCredentials(Agent agent, Account account, Map<String, Long> externalLinks, Map<String, ExternalProject> projects, Map<Long, Region> regionsIds, Map<String, Region> regionNameToRegion) { Region localRegion = null; for (Region region : regionsIds.values()) { if (region.getLocal()) { localRegion = region; } } boolean success = true; // 2. Reconcile agent's credentials try { reconcileExternalCredentials(account, agent, localRegion, externalLinks, projects, regionNameToRegion); } catch (Exception ex) { success = false; log.error(String.format("Fail to reconcile credentials for agent [%d]", agent.getId()), ex); } return success; }
private void cleanupExternalLinks(Region region) { for (AccountLink link : objectManager.find(AccountLink.class, ACCOUNT_LINK.REMOVED, null, ACCOUNT_LINK.LINKED_REGION_ID, region.getId())) { if (!link.getState().equalsIgnoreCase(CommonStatesConstants.REMOVING)) { objectProcessManager.executeStandardProcess(StandardProcess.REMOVE, link, null); } } } }
public MetaHelperInfo(Account account, Map<Long, Account> accounts, Set<Long> otherAccountsServicesIds, Set<Long> otherAccountsStackIds, MetaDataInfoDao dao, Region regionObj) { super(); this.accounts = accounts; this.otherAccountsServicesIds = otherAccountsServicesIds; this.otherAccountsStackIds = otherAccountsStackIds; this.account = account; // may be fix in the future - get health checker hosts ids for instances of other accounts // otherwise this info is irrelevant this.instanceIdToHealthCheckers = dao.getInstanceIdToHealthCheckers(account); this.hostIdToHostMetadata = dao.getHostIdToHostMetadata(account, accounts, otherAccountsServicesIds); if (regionObj != null) { regionName = regionObj.getName(); } }
private void getLinkedEnvironments(long accountId, Map<String, Long> links, Map<Long, Region> regionsIds, Map<String, ExternalProject> externalProjects) { List<AccountLink> accountLinks = objectManager.find(AccountLink.class, ACCOUNT_LINK.ACCOUNT_ID, accountId, ACCOUNT_LINK.REMOVED, null, ACCOUNT_LINK.LINKED_REGION_ID, new Condition(ConditionType.NOTNULL)); for (AccountLink link : accountLinks) { if (link.getState().equalsIgnoreCase(CommonStatesConstants.REMOVING)) { continue; } if(link.getState().equalsIgnoreCase(CommonStatesConstants.REMOVED)) { continue; } Region targetRegion = regionsIds.get(link.getLinkedRegionId()); if (targetRegion == null) { continue; } String UUID = getUUID(targetRegion.getName(), link.getLinkedAccount()); if (!externalProjects.containsKey(UUID)) { links.put(UUID, targetRegion.getId()); } } }
@Override public void fetchCredentials(MetaHelperInfo helperInfo, Agent agent, OutputStream os) { Map<Long, Region> regionsIds = new HashMap<>(); Map<String, Region> regionNameToRegion = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); Map<String, Long> externalLinks = new HashMap<>(); Map<String, ExternalProject> projects = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); if(regionService.isRegionsEmpty(agent, helperInfo.getAccount(), externalLinks, projects, regionsIds, regionNameToRegion)) { return; } regionService.reconcileAgentExternalCredentials(agent, helperInfo.getAccount(), externalLinks, projects, regionsIds, regionNameToRegion); List<ExternalCredential> creds = DataAccessor.fieldObjectList(objectMgr.reload(agent), AccountConstants.FIELD_EXTERNAL_CREDENTIALS, ExternalCredential.class, jsonMapper); for (ExternalCredential cred : creds) { Region region = regionNameToRegion.get(cred.getRegionName()); CredentialMetaData meta = new CredentialMetaData(region.getUrl(), cred.getPublicValue(), cred.getSecretValue()); writeToJson(os, meta); } } }
@Override public boolean deactivateAndRemoveExternalAgent(Agent agent) { List<? extends ExternalCredential> creds = DataAccessor.fieldObjectList(agent, AccountConstants.FIELD_EXTERNAL_CREDENTIALS, ExternalCredential.class, jsonMapper); if (creds.isEmpty()) { return true; } Map<String, Region> regions = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); for (Region region : objectManager.find(Region.class, REGION.REMOVED, new Condition(ConditionType.NULL))) { regions.put(region.getName(), region); } for (ExternalCredential cred : creds) { if (!deactivateAndRemoveExternalAgent(agent, cred)) { return false; } } return true; } }
String UUID = getUUID(targetRegion.getName(), cred.getEnvironmentName()); ExternalProject targetResourceAccount = null; if (externalProjects.containsKey(UUID)) { if (targetResourceAccount == null) { throw new RuntimeException(String.format("Failed to find target environment by name [%s] in region [%s]", cred.getEnvironmentName(), targetRegion.getName())); String targetAgentUri = RegionUtil.getTargetAgentUri(localRegion.getName(), account.getName(), agent.getUuid(), targetResourceAccount.getUuid()); log.info(String.format("Creating external agent with uri [%s] in environment [%s] in region [%s]", targetAgentUri,
return; ExternalRegion externalRegion = RegionUtil.getExternalRegion(targetRegion, localRegion.getName(), jsonMapper); if (externalRegion == null) { throw new RuntimeException(String.format("Failed to find local region [%s] in external region [%s]", localRegion.getName(), targetRegion.getName())); if (targetResourceAccount == null) { throw new RuntimeException(String.format("Failed to find target environment by name [%s] in region [%s]", link.getLinkedAccount(), localRegion.getName()));
String externalRegionKey = String.format("%s:%s", targetRegion.getName(), localRegion.getName()); if(externalRegionMap.containsKey(externalRegionKey)) { externalRegion = externalRegionMap.get(externalRegionKey); } else { externalRegion = RegionUtil.getExternalRegion(targetRegion, localRegion.getName(), jsonMapper); externalRegionMap.put(externalRegionKey, externalRegion); String externalProjectKey = String.format("%s:%s", targetRegion.getName(), link.getLinkedAccount()); if(externalProjectMap.containsKey(externalProjectKey)) { externalProjectResponse = externalProjectMap.get(externalProjectKey);