Added PBM service connect

This commit is contained in:
Harikrishna Patnala 2020-05-26 19:09:40 +05:30
parent 0701dc9d9c
commit f05b567d4c
4 changed files with 145 additions and 3 deletions

View File

@ -1,6 +1,21 @@
// 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 com.cloud.hypervisor.vmware.mo;
import com.cloud.hypervisor.vmware.util.VmwareContext;
import com.vmware.pbm.PbmProfile;

View File

@ -0,0 +1,88 @@
// 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 com.cloud.hypervisor.vmware.util;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import org.apache.log4j.Logger;
import org.w3c.dom.DOMException;
import com.cloud.utils.exception.CloudRuntimeException;
public class VcenterSessionHandler implements SOAPHandler<SOAPMessageContext> {
public static final Logger s_logger = Logger.getLogger(VcenterSessionHandler.class);
private final String vcSessionCookie;
public VcenterSessionHandler(String vcSessionCookie) {
this.vcSessionCookie = vcSessionCookie;
}
@Override
public boolean handleMessage(SOAPMessageContext smc) {
if (isOutgoingMessage(smc)) {
try {
SOAPHeader header = getSOAPHeader(smc);
SOAPElement vcsessionHeader = header.addChildElement(new javax.xml.namespace.QName("#",
"vcSessionCookie"));
vcsessionHeader.setValue(vcSessionCookie);
} catch (DOMException e) {
s_logger.debug(e);
throw new CloudRuntimeException(e);
} catch (SOAPException e) {
s_logger.debug(e);
throw new CloudRuntimeException(e);
}
}
return true;
}
@Override
public void close(MessageContext arg0) {
}
@Override
public boolean handleFault(SOAPMessageContext arg0) {
return false;
}
@Override
public Set<QName> getHeaders() {
return null;
}
SOAPHeader getSOAPHeader(SOAPMessageContext smc) throws SOAPException {
return smc.getMessage().getSOAPPart().getEnvelope().getHeader() == null ? smc
.getMessage().getSOAPPart().getEnvelope().addHeader()
: smc.getMessage().getSOAPPart().getEnvelope().getHeader();
}
boolean isOutgoingMessage(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean)smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
return outboundProperty;
}
}

View File

@ -17,8 +17,11 @@
package com.cloud.hypervisor.vmware.util;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
@ -29,10 +32,15 @@ import javax.net.ssl.SSLSession;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.HandlerResolver;
import javax.xml.ws.handler.PortInfo;
import org.apache.cloudstack.utils.security.SSLUtils;
import org.apache.cloudstack.utils.security.SecureSSLSocketFactory;
import com.vmware.pbm.PbmPortType;
import com.vmware.pbm.PbmService;
import com.vmware.pbm.PbmServiceInstanceContent;
import org.apache.log4j.Logger;
import org.w3c.dom.Element;
@ -103,6 +111,7 @@ public class VmwareClient {
HttpsURLConnection.setDefaultHostnameVerifier(hv);
vimService = new VimService();
pbmService = new PbmService();
} catch (Exception e) {
s_logger.info("[ignored]"
+ "failed to trust all certificates blindly: ", e);
@ -125,6 +134,8 @@ public class VmwareClient {
private final ManagedObjectReference pbmSvcInstRef = new ManagedObjectReference();
private static VimService vimService;
private static PbmService pbmService;
private PbmServiceInstanceContent pbmServiceContent;
private VimPortType vimPort;
private PbmPortType pbmPort;
private static final String PBM_SERVICE_INSTANCE_TYPE = "PbmServiceInstance";
@ -184,10 +195,38 @@ public class VmwareClient {
cookieValue = tokenizer.nextToken();
String pathData = "$" + tokenizer.nextToken();
serviceCookie = "$Version=\"1\"; " + cookieValue + "; " + pathData;
Map<String, List<String>> map = new HashMap<String, List<String>>();
map.put("Cookie", Collections.singletonList(serviceCookie));
((BindingProvider)vimPort).getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, map);
pbmConnect(url, cookieValue);
isConnected = true;
}
private void pbmConnect(String url, String cookieValue) throws Exception {
URI uri = new URI(url);
String pbmurl = "https://" + uri.getHost() + "/pbm";
String[] tokens = cookieValue.split("=");
String extractedCookie = tokens[1];
HandlerResolver soapHandlerResolver = new HandlerResolver() {
@Override
public List<Handler> getHandlerChain(PortInfo portInfo) {
VcenterSessionHandler VcSessionHandler = new VcenterSessionHandler(extractedCookie);
List<Handler> handlerChain = new ArrayList<Handler>();
handlerChain.add((Handler)VcSessionHandler);
return handlerChain;
}
};
pbmService.setHandlerResolver(soapHandlerResolver);
pbmSvcInstRef.setType(PBM_SERVICE_INSTANCE_TYPE);
pbmSvcInstRef.setValue(PBM_SERVICE_INSTANCE_VALUE);
pbmPort = pbmService.getPbmPort();
Map<String, Object> pbmCtxt = ((BindingProvider)pbmPort).getRequestContext();
pbmCtxt.put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
pbmCtxt.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, pbmurl);
}
/**
* Disconnects the user session.
*