DaanHoogland commented on code in PR #7996: URL: https://github.com/apache/cloudstack/pull/7996#discussion_r1338334478
########## engine/schema/src/main/resources/META-INF/db/schema-41810to41900.sql: ########## @@ -180,3 +180,28 @@ CREATE TABLE `cloud`.`vm_scheduled_job` ( -- Add support for different cluster types for kubernetes ALTER TABLE `cloud`.`kubernetes_cluster` ADD COLUMN `cluster_type` varchar(64) DEFAULT 'CloudManaged' COMMENT 'type of cluster'; ALTER TABLE `cloud`.`kubernetes_cluster` MODIFY COLUMN `kubernetes_version_id` bigint unsigned NULL COMMENT 'the ID of the Kubernetes version of this Kubernetes cluster'; + +UPDATE `cloud`.`configuration` SET + `kind` = 'Order', + `options` = 'PBKDF2,SHA256SALT,MD5,LDAP,SAML2,PLAINTEXT,OAUTH2' +where `name` = 'user.authenticators.order' ; Review Comment: this will change the order for people that have modified this configuration item! ########## engine/schema/src/main/resources/META-INF/db/schema-41810to41900.sql: ########## @@ -180,3 +180,28 @@ CREATE TABLE `cloud`.`vm_scheduled_job` ( -- Add support for different cluster types for kubernetes ALTER TABLE `cloud`.`kubernetes_cluster` ADD COLUMN `cluster_type` varchar(64) DEFAULT 'CloudManaged' COMMENT 'type of cluster'; ALTER TABLE `cloud`.`kubernetes_cluster` MODIFY COLUMN `kubernetes_version_id` bigint unsigned NULL COMMENT 'the ID of the Kubernetes version of this Kubernetes cluster'; + +UPDATE `cloud`.`configuration` SET + `kind` = 'Order', + `options` = 'PBKDF2,SHA256SALT,MD5,LDAP,SAML2,PLAINTEXT,OAUTH2' +where `name` = 'user.authenticators.order' ; + +UPDATE `cloud`.`configuration` SET + `kind` = 'Order', + `options` = 'SAML2Auth,OAUTH2Auth' +where `name` = 'pluggableApi.authenticators.order' ; Review Comment: idem ########## api/src/main/java/org/apache/cloudstack/auth/UserOAuth2Authenticator.java: ########## @@ -0,0 +1,40 @@ +// 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.auth; + +import com.cloud.utils.component.Adapter; + +public interface UserOAuth2Authenticator extends Adapter { Review Comment: nice use of java docs in this class :+1: ########## plugins/user-authenticators/oauth2/src/main/java/org/apache/cloudstack/oauth2/api/command/OauthLoginAPIAuthenticatorCmd.java: ########## @@ -0,0 +1,221 @@ +// 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.oauth2.api.command; + +import com.cloud.api.ApiServlet; +import com.cloud.domain.Domain; +import com.cloud.user.User; +import com.cloud.user.UserAccount; +import org.apache.cloudstack.api.ApiServerService; +import com.cloud.api.response.ApiResponseSerializer; +import com.cloud.exception.CloudAuthenticationException; +import com.cloud.user.Account; +import org.apache.cloudstack.api.APICommand; +import org.apache.cloudstack.api.ApiConstants; +import org.apache.cloudstack.api.ApiErrorCode; +import org.apache.cloudstack.api.BaseCmd; +import org.apache.cloudstack.api.Parameter; +import org.apache.cloudstack.api.ServerApiException; +import org.apache.cloudstack.api.auth.APIAuthenticationType; +import org.apache.cloudstack.api.auth.APIAuthenticator; +import org.apache.cloudstack.api.auth.PluggableAPIAuthenticator; +import org.apache.cloudstack.api.response.LoginCmdResponse; +import org.apache.commons.lang3.StringUtils; +import org.apache.log4j.Logger; +import org.jetbrains.annotations.Nullable; + +import javax.inject.Inject; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.util.List; +import java.util.Map; +import java.net.InetAddress; + +import static org.apache.cloudstack.oauth2.OAuth2AuthManager.OAuth2IsPluginEnabled; + +@APICommand(name = "oauthlogin", description = "Logs a user into the CloudStack after successful verification of OAuth secret code from the particular provider." + + "A successful login attempt will generate a JSESSIONID cookie value that can be passed in subsequent Query command calls until the \"logout\" command has been issued or the session has expired.", + requestHasSensitiveInfo = true, responseObject = LoginCmdResponse.class, entityType = {}, since = "4.19.0") +public class OauthLoginAPIAuthenticatorCmd extends BaseCmd implements APIAuthenticator { + + public static final Logger s_logger = Logger.getLogger(OauthLoginAPIAuthenticatorCmd.class.getName()); + + ///////////////////////////////////////////////////// + //////////////// API parameters ///////////////////// + ///////////////////////////////////////////////////// + @Parameter(name = ApiConstants.PROVIDER, type = CommandType.STRING, description = "Name of the provider", required = true) + private String provider; + + @Parameter(name = ApiConstants.EMAIL, type = CommandType.STRING, description = "Email id with which user tried to login using OAuth provider", required = true) + private String email; + + @Parameter(name = ApiConstants.DOMAIN, type = CommandType.STRING, description = "Path of the domain that the user belongs to. Example: domain=/com/cloud/internal. If no domain is passed in, the ROOT (/) domain is assumed.") + private String domain; + + @Parameter(name = ApiConstants.DOMAIN__ID, type = CommandType.LONG, description = "The id of the domain that the user belongs to. If both domain and domainId are passed in, \"domainId\" parameter takes precedence.") + private Long domainId; + + @Parameter(name = ApiConstants.SECRET_CODE, type = CommandType.STRING, description = "Code that is provided by OAuth provider (Eg. google, github) after successful login") + private String secretCode; + + @Inject + ApiServerService _apiServer; + + ///////////////////////////////////////////////////// + /////////////////// Accessors /////////////////////// + ///////////////////////////////////////////////////// + + public String getProvider() { + return provider; + } + + public String getEmail() { + return email; + } + + public String getDomainName() { + return domain; + } + + public Long getDomainId() { + return domainId; + } + + public String getSecretCode() { + return secretCode; + } + + ///////////////////////////////////////////////////// + /////////////// API Implementation/////////////////// + ///////////////////////////////////////////////////// + + @Override + public long getEntityOwnerId() { + return Account.Type.NORMAL.ordinal(); + } + + @Override + public void execute() throws ServerApiException { + // We should never reach here + throw new ServerApiException(ApiErrorCode.METHOD_NOT_ALLOWED, "This is an authentication api, cannot be used directly"); + } + + @Override + public String authenticate(String command, Map<String, Object[]> params, HttpSession session, InetAddress remoteAddress, String responseType, StringBuilder auditTrailSb, final HttpServletRequest req, final HttpServletResponse resp) throws ServerApiException { Review Comment: can you disect this method is smaller (more comprehensible and testable) chunks? ########## engine/schema/src/main/resources/META-INF/db/schema-41810to41900.sql: ########## @@ -180,3 +180,28 @@ CREATE TABLE `cloud`.`vm_scheduled_job` ( -- Add support for different cluster types for kubernetes ALTER TABLE `cloud`.`kubernetes_cluster` ADD COLUMN `cluster_type` varchar(64) DEFAULT 'CloudManaged' COMMENT 'type of cluster'; ALTER TABLE `cloud`.`kubernetes_cluster` MODIFY COLUMN `kubernetes_version_id` bigint unsigned NULL COMMENT 'the ID of the Kubernetes version of this Kubernetes cluster'; + +UPDATE `cloud`.`configuration` SET + `kind` = 'Order', + `options` = 'PBKDF2,SHA256SALT,MD5,LDAP,SAML2,PLAINTEXT,OAUTH2' +where `name` = 'user.authenticators.order' ; Review Comment: Not sure if this will work, but I think we need something like ```suggestion UPDATE `cloud`.`configuration` SET `kind` = 'Order', `options` = concat(SELECT `options` FROM `cloud`.`configuration` WHERE `name` = 'user.authenticators.order', 'OAUTH2') WHERE `name` = 'user.authenticators.order' ; ``` ########## plugins/user-authenticators/oauth2/src/main/java/org/apache/cloudstack/oauth2/google/GoogleOAuth2Utils.java: ########## @@ -0,0 +1,97 @@ +// +// 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.oauth2.google; + +import com.cloud.utils.PropertiesUtil; +import com.cloud.utils.server.ServerProperties; +import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; +import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken; +import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier; +import com.google.api.client.http.HttpTransport; +import com.google.api.client.http.javanet.NetHttpTransport; +import com.google.api.client.json.gson.GsonFactory; +import com.google.api.client.json.jackson2.JacksonFactory; +import com.google.api.client.util.store.MemoryDataStoreFactory; +import org.apache.log4j.Logger; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.security.GeneralSecurityException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Properties; + +public class GoogleOAuth2Utils { + + private static final Logger s_logger = Logger.getLogger(GoogleOAuth2Utils.class); + + private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); + + public static GoogleAuthorizationCodeFlow newFlow() throws IOException { + String oauthClientId = null; + String oauthClientSecret = null; + + final File confFile = PropertiesUtil.findConfigFile("server.properties"); + + s_logger.info("Server configuration file found: " + confFile.getAbsolutePath()); + + try { + InputStream is = new FileInputStream(confFile); + final Properties properties = ServerProperties.getServerProperties(is); + + oauthClientId = "345798102268-cfcpg40k6hnfft2m61mf6jbmjcfg4p82.apps.googleusercontent.com"; + oauthClientSecret = "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"; + } catch (final IOException e) { + } Review Comment: ```suggestion } catch (final IOException e) { s_logger.warn("Google OAuth2 properties could not be read"); } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
