-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Adding new Dell EMC ECS Object Storage Plugin for CloudStack #12124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mhkadhum
wants to merge
17
commits into
apache:main
Choose a base branch
from
mhkadhum:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,572
−70
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
8f36ebf
Adding New Dell ECS Plugin to the API & editing Frontend for the ECS …
f131d41
Update AddObjectStorage.vue ECS Field Names and desc.
mhkadhum 625fb4e
Merge branch 'apache:main' into main
mhkadhum 124649f
add missing license
ce40daf
reverting update by pre-commit on tools/marvin/setup.py
mhkadhum 7dd1bf2
adding ECS Module in the Plugins pom.xml
mhkadhum 6e3fb3e
Update ECS Driver, split to small helpers , remove SuppressWarnings f…
a10e06e
Merge branch 'apache:main' into main
mhkadhum 6019139
Update ECS Plugin: address reviewer comments for ECS Plugin
1ab9434
adding license to ecs new files
mhkadhum 268eb21
adding license to ecs new files
mhkadhum 9941137
adding license to ecs new files
mhkadhum af3e8cf
fixing spaces
mhkadhum ba4af0c
Update EcsObjectStoreDriverImpl.java
mhkadhum 7c86383
Update EcsObjectStoreDriverImpl.java
mhkadhum b1d5ecd
Update versioning logic, adress the reviwer comment for the UI
03a7134
add missing lice.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <!-- | ||
| Licensed to the Apache Software Foundation (ASF) under one | ||
| or more contributor license agreements. See the NOTICE file | ||
| distributed with this work for additional information | ||
| regarding copyright ownership. The ASF licenses this file | ||
| to you under the Apache License, Version 2.0 (the | ||
| "License"); you may not use this file except in compliance | ||
| with the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, | ||
| software distributed under the License is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations | ||
| under the License. | ||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <artifactId>cloud-plugin-storage-object-ecs</artifactId> | ||
| <name>Apache CloudStack Plugin - ECS object storage provider</name> | ||
| <parent> | ||
| <groupId>org.apache.cloudstack</groupId> | ||
| <artifactId>cloudstack-plugins</artifactId> | ||
| <version>4.23.0.0-SNAPSHOT</version> | ||
| <relativePath>../../../pom.xml</relativePath> | ||
| </parent> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.apache.cloudstack</groupId> | ||
| <artifactId>cloud-engine-storage</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.cloudstack</groupId> | ||
| <artifactId>cloud-engine-storage-object</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.cloudstack</groupId> | ||
| <artifactId>cloud-engine-schema</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> |
38 changes: 38 additions & 0 deletions
38
...object/ECS/src/main/java/org/apache/cloudstack/storage/datastore/driver/EcsConstants.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.cloudstack.storage.datastore.driver; | ||
|
|
||
| public final class EcsConstants { | ||
| private EcsConstants() {} | ||
|
|
||
| // Object store details keys | ||
| public static final String MGMT_URL = "mgmt_url"; | ||
| public static final String SA_USER = "sa_user"; | ||
| public static final String SA_PASS = "sa_password"; | ||
| public static final String NAMESPACE = "namespace"; | ||
| public static final String INSECURE = "insecure"; | ||
| public static final String S3_HOST = "s3_host"; | ||
| public static final String USER_PREFIX = "user_prefix"; | ||
| public static final String DEFAULT_USER_PREFIX = "cs-"; | ||
|
|
||
| // Per-account keys | ||
| public static final String AD_KEY_ACCESS = "ecs.accesskey"; | ||
| public static final String AD_KEY_SECRET = "ecs.secretkey"; | ||
| } |
161 changes: 161 additions & 0 deletions
161
...ECS/src/main/java/org/apache/cloudstack/storage/datastore/driver/EcsMgmtTokenManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.apache.cloudstack.storage.datastore.driver; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| import org.apache.http.auth.UsernamePasswordCredentials; | ||
| import org.apache.http.client.methods.CloseableHttpResponse; | ||
| import org.apache.http.client.methods.HttpGet; | ||
| import org.apache.http.impl.auth.BasicScheme; | ||
| import org.apache.http.impl.client.CloseableHttpClient; | ||
| import org.apache.http.util.EntityUtils; | ||
|
|
||
| import com.cloud.utils.exception.CloudRuntimeException; | ||
|
|
||
| public class EcsMgmtTokenManager { | ||
| private static final long DEFAULT_TOKEN_MAX_AGE_SEC = 300; | ||
| private static final long EXPIRY_SKEW_SEC = 30; | ||
|
|
||
| private static final ConcurrentHashMap<TokenKey, TokenEntry> TOKEN_CACHE = new ConcurrentHashMap<>(); | ||
| private static final ConcurrentHashMap<TokenKey, Object> TOKEN_LOCKS = new ConcurrentHashMap<>(); | ||
|
|
||
| static final class EcsUnauthorizedException extends RuntimeException { | ||
| EcsUnauthorizedException(final String msg) { super(msg); } | ||
| } | ||
|
|
||
| @FunctionalInterface | ||
| public interface WithToken<T> { T run(String token) throws Exception; } | ||
|
|
||
| private static final class TokenKey { | ||
| final String mgmtUrl; | ||
| final String user; | ||
| TokenKey(final String mgmtUrl, final String user) { | ||
| this.mgmtUrl = mgmtUrl; | ||
| this.user = user; | ||
| } | ||
| @Override public boolean equals(final Object o) { | ||
| if (this == o) return true; | ||
| if (!(o instanceof TokenKey)) return false; | ||
| final TokenKey k = (TokenKey) o; | ||
| return Objects.equals(mgmtUrl, k.mgmtUrl) && Objects.equals(user, k.user); | ||
| } | ||
| @Override public int hashCode() { return Objects.hash(mgmtUrl, user); } | ||
| } | ||
|
|
||
| private static final class TokenEntry { | ||
| final String token; | ||
| final long expiresAtMs; | ||
| TokenEntry(final String token, final long expiresAtMs) { | ||
| this.token = token; | ||
| this.expiresAtMs = expiresAtMs; | ||
| } | ||
| boolean validNow() { | ||
| return token != null && !token.isBlank() && System.currentTimeMillis() < expiresAtMs; | ||
| } | ||
| } | ||
|
|
||
| public <T> T callWithRetry401(final EcsObjectStoreDriverImpl.EcsCfg cfg, | ||
| final WithToken<T> op, | ||
| final HttpClientFactory httpFactory) throws Exception { | ||
| try { | ||
| return op.run(getAuthToken(cfg, httpFactory)); | ||
| } catch (EcsUnauthorizedException u) { | ||
| invalidate(cfg); | ||
| return op.run(getAuthToken(cfg, httpFactory)); | ||
| } | ||
| } | ||
|
|
||
| public void invalidate(final EcsObjectStoreDriverImpl.EcsCfg cfg) { | ||
| TOKEN_CACHE.remove(new TokenKey(trimTail(cfg.mgmtUrl), cfg.saUser)); | ||
| } | ||
|
|
||
| public String getAuthToken(final EcsObjectStoreDriverImpl.EcsCfg cfg, | ||
| final HttpClientFactory httpFactory) { | ||
| final String mu = trimTail(cfg.mgmtUrl); | ||
| final TokenKey key = new TokenKey(mu, cfg.saUser); | ||
|
|
||
| final TokenEntry cached = TOKEN_CACHE.get(key); | ||
| if (cached != null && cached.validNow()) return cached.token; | ||
|
|
||
| final Object lock = TOKEN_LOCKS.computeIfAbsent(key, k -> new Object()); | ||
| synchronized (lock) { | ||
| final TokenEntry cached2 = TOKEN_CACHE.get(key); | ||
| if (cached2 != null && cached2.validNow()) return cached2.token; | ||
|
|
||
| final TokenEntry fresh = loginAndGetTokenFresh(mu, cfg.saUser, cfg.saPass, cfg.insecure, httpFactory); | ||
| TOKEN_CACHE.put(key, fresh); | ||
| return fresh.token; | ||
| } | ||
| } | ||
|
|
||
| private TokenEntry loginAndGetTokenFresh(final String mgmtUrl, | ||
| final String user, | ||
| final String pass, | ||
| final boolean insecure, | ||
| final HttpClientFactory httpFactory) { | ||
| try (CloseableHttpClient http = httpFactory.build(insecure)) { | ||
| final HttpGet get = new HttpGet(mgmtUrl + "/login"); | ||
| UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, pass); | ||
| get.addHeader(new BasicScheme().authenticate(creds, get, null)); | ||
|
|
||
| try (CloseableHttpResponse resp = http.execute(get)) { | ||
| final int status = resp.getStatusLine().getStatusCode(); | ||
| if (status != 200 && status != 201) { | ||
| final String body = resp.getEntity() != null | ||
| ? EntityUtils.toString(resp.getEntity(), StandardCharsets.UTF_8) | ||
| : ""; | ||
| throw new CloudRuntimeException("ECS /login failed: HTTP " + status + " body=" + body); | ||
| } | ||
| if (resp.getFirstHeader("X-SDS-AUTH-TOKEN") == null) { | ||
| throw new CloudRuntimeException("ECS /login did not return X-SDS-AUTH-TOKEN header"); | ||
| } | ||
|
|
||
| final String token = resp.getFirstHeader("X-SDS-AUTH-TOKEN").getValue(); | ||
|
|
||
| long maxAgeSec = DEFAULT_TOKEN_MAX_AGE_SEC; | ||
| try { | ||
| if (resp.getFirstHeader("X-SDS-AUTH-MAX-AGE") != null) { | ||
| maxAgeSec = Long.parseLong(resp.getFirstHeader("X-SDS-AUTH-MAX-AGE").getValue().trim()); | ||
| } | ||
| } catch (Exception ignore) { } | ||
|
|
||
| final long effectiveSec = Math.max(5, maxAgeSec - EXPIRY_SKEW_SEC); | ||
| final long expiresAtMs = System.currentTimeMillis() + (effectiveSec * 1000L); | ||
| return new TokenEntry(token, expiresAtMs); | ||
| } | ||
| } catch (Exception e) { | ||
| throw new CloudRuntimeException("Failed to obtain ECS auth token: " + e.getMessage(), e); | ||
| } | ||
| } | ||
|
|
||
| private static String trimTail(final String s) { | ||
| if (s == null) return null; | ||
| return s.endsWith("/") ? s.substring(0, s.length() - 1) : s; | ||
| } | ||
|
|
||
| /** Simple seam for testability; implemented by the driver using its existing buildHttpClient(). */ | ||
| @FunctionalInterface | ||
| public interface HttpClientFactory { | ||
| CloseableHttpClient build(boolean insecure); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this method contains three levels of try blocks. the second level has no catch or final blocks. @mhkadhum , are you sure about this? I’d try to dissect it in smaller methods (no 👎 !)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the first two try-with-resources blocks which have a finally to close the resources so they dont need a catch or finally. The third try is only for parsing the header so its valid try-with-resources i would like to keep it as it is for now