Adds module code structure, build script, test script, and API bindings

This commit is contained in:
dmilov
2020-09-28 11:25:47 +03:00
parent fe6932a5fe
commit 64e0b52224
28 changed files with 3122 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
**/.vs
**/bin
**/obj

View File

@@ -0,0 +1,24 @@
{
"ProviderId": "Microsoft.VisualStudio.ConnectedService.Wcf",
"Version": "15.0.20628.921",
"ExtendedData": {
"Uri": "https://10.23.80.205/lookupservice/wsdl/lookup.wsdl",
"Namespace": "LookupServiceReference",
"SelectedAccessLevelForGeneratedClass": "Public",
"GenerateMessageContract": false,
"ReuseTypesinReferencedAssemblies": true,
"ReuseTypesinAllReferencedAssemblies": true,
"CollectionTypeReference": {
"Item1": "System.Array",
"Item2": "System.Runtime.dll"
},
"DictionaryCollectionTypeReference": {
"Item1": "System.Collections.Generic.Dictionary`2",
"Item2": "System.Collections.dll"
},
"CheckedReferencedAssemblies": [],
"InstanceId": null,
"Name": "LookupServiceReference",
"Metadata": {}
}
}

View File

@@ -0,0 +1,129 @@
// **************************************************************************
// Copyright 2019 VMware, Inc. All rights reserved. -- VMware Confidential.
// **************************************************************************
using System;
using System.Collections;
using System.Collections.Generic;
using System.IdentityModel.Selectors;
using System.Linq;
using System.Security;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Security;
using System.Text;
using LookupServiceReference;
namespace VMware.vSphere.LsClient
{
public class LookupServiceClient {
private const int WEB_OPERATION_TIMEOUT_SECONDS = 30;
private LsPortTypeClient _lsClient;
private static readonly ManagedObjectReference RootMoRef = new ManagedObjectReference
{
type = "LookupServiceInstance",
Value = "ServiceInstance"
};
public LookupServiceClient(string hostname, X509CertificateValidator serverCertificateValidator) {
var lsUri = $"https://{hostname}/lookupservice/sdk";
_lsClient = new LsPortTypeClient(GetBinding(), new EndpointAddress(new Uri(lsUri)));
var serverAuthentication = GetServerAuthentication(serverCertificateValidator);
if (serverAuthentication != null)
{
_lsClient
.ChannelFactory
.Credentials
.ServiceCertificate
.SslCertificateAuthentication = serverAuthentication;
}
}
#region Private Helpers
private X509ServiceCertificateAuthentication GetServerAuthentication(X509CertificateValidator serverCertificateValidator)
{
if (serverCertificateValidator != null) {
return new X509ServiceCertificateAuthentication {
CertificateValidationMode = X509CertificateValidationMode.Custom,
CustomCertificateValidator = serverCertificateValidator
};
}
// Default .NET behavior for TLS certificate validation
return null;
}
private static MessageEncodingBindingElement GetWcfEncoding()
{
return new TextMessageEncodingBindingElement(MessageVersion.Soap11, Encoding.UTF8);
}
private static HttpsTransportBindingElement GetWcfTransport(bool useSystemProxy)
{
HttpsTransportBindingElement transport = new HttpsTransportBindingElement
{
RequireClientCertificate = false
};
transport.UseDefaultWebProxy = useSystemProxy;
transport.MaxBufferSize = 2147483647;
transport.MaxReceivedMessageSize = 2147483647;
return transport;
}
private static Binding GetBinding() {
var binding = new CustomBinding(GetWcfEncoding(), GetWcfTransport(true));
var timeout = TimeSpan.FromSeconds(WEB_OPERATION_TIMEOUT_SECONDS);
binding.CloseTimeout = timeout;
binding.OpenTimeout = timeout;
binding.ReceiveTimeout = timeout;
binding.SendTimeout = timeout;
return binding;
}
#endregion
public Uri GetSsoAdminEndpointUri() {
var product = "com.vmware.cis";
var endpointType = "com.vmware.cis.cs.identity.admin";
var type = "sso:admin";
return FindServiceEndpoint(product, type, endpointType);
}
private Uri FindServiceEndpoint(string product, string type, string endpointType) {
Uri result = null;
var svcContent = _lsClient.RetrieveServiceContentAsync(RootMoRef).Result;
var filterCriteria = new LookupServiceRegistrationFilter() {
searchAllSsoDomains = true,
serviceType = new LookupServiceRegistrationServiceType {
product = product,
type = type
}
};
var lsRegInfo = _lsClient.
ListAsync(svcContent.serviceRegistration, filterCriteria)
.Result?
.returnval?
.FirstOrDefault();
if (lsRegInfo != null) {
var registrationEndpooint = lsRegInfo.
serviceEndpoints?.
Where(a => a.endpointType.type == endpointType)?.
FirstOrDefault<LookupServiceRegistrationEndpoint>();
if (registrationEndpooint != null) {
result = new Uri(registrationEndpooint.url);
}
}
return result;
}
}
}

View File

@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RootNamespace>VMware.vSphere.LsClient</RootNamespace>
<AssemblyName>VMware.vSphere.LsClient</AssemblyName>
<Description>vSphere Lookup Service API client.</Description>
<TargetFrameworks>net45;netcoreapp2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
<Reference Include="System.IdentityModel" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.ServiceModel.Primitives" Version="4.4.0" />
<PackageReference Include="System.ServiceModel.Duplex" Version="4.4.0" />
<PackageReference Include="System.ServiceModel.Http" Version="4.4.0" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.4.0" />
<PackageReference Include="System.ServiceModel.Security" Version="4.4.0" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Connected Services" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,37 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30503.244
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VMware.vSphere.SsoAdminClient", "VMware.vSphere.SsoAdminClient\VMware.vSphere.SsoAdminClient.csproj", "{BD48E0DD-4048-48FD-B0BE-560E2417A2CC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VMware.vSphere.LsClient", "VMware.vSphere.LsClient\VMware.vSphere.LsClient.csproj", "{EEC4C335-3E6C-4FA5-84CD-CBADCD720F35}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VMware.vSphere.SsoAdmin.Utils", "VMware.vSphere.SsoAdmin.Utils\VMware.vSphere.SsoAdmin.Utils.csproj", "{1523743E-C01E-4D37-845F-0BB8DAF9EE7E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BD48E0DD-4048-48FD-B0BE-560E2417A2CC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BD48E0DD-4048-48FD-B0BE-560E2417A2CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD48E0DD-4048-48FD-B0BE-560E2417A2CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD48E0DD-4048-48FD-B0BE-560E2417A2CC}.Release|Any CPU.Build.0 = Release|Any CPU
{EEC4C335-3E6C-4FA5-84CD-CBADCD720F35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EEC4C335-3E6C-4FA5-84CD-CBADCD720F35}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EEC4C335-3E6C-4FA5-84CD-CBADCD720F35}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EEC4C335-3E6C-4FA5-84CD-CBADCD720F35}.Release|Any CPU.Build.0 = Release|Any CPU
{1523743E-C01E-4D37-845F-0BB8DAF9EE7E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1523743E-C01E-4D37-845F-0BB8DAF9EE7E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1523743E-C01E-4D37-845F-0BB8DAF9EE7E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1523743E-C01E-4D37-845F-0BB8DAF9EE7E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9A376526-4487-43FF-A527-E34AD4764F12}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,16 @@
using System;
using System.IdentityModel.Selectors;
using System.Security.Cryptography.X509Certificates;
namespace VMware.vSphere.SsoAdmin.Utils
{
public class AcceptAllX509CertificateValidator : X509CertificateValidator
{
public override void Validate(X509Certificate2 certificate) {
// Check that there is a certificate.
if (certificate == null) {
throw new ArgumentNullException(nameof(certificate));
}
}
}
}

View File

@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RootNamespace>VMware.vSphere.SsoAdmin.Utils</RootNamespace>
<AssemblyName>VMware.vSphere.SsoAdmin.Utils</AssemblyName>
<Description>vSphere Lookup SsoAdmin utility types.</Description>
<TargetFrameworks>net45;netcoreapp2.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
<Reference Include="System.IdentityModel" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.ServiceModel.Primitives" Version="4.4.0" />
<PackageReference Include="System.ServiceModel.Duplex" Version="4.4.0" />
<PackageReference Include="System.ServiceModel.Http" Version="4.4.0" />
<PackageReference Include="System.ServiceModel.NetTcp" Version="4.4.0" />
<PackageReference Include="System.ServiceModel.Security" Version="4.4.0" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,8 @@
using System;
namespace VMware.vSphere.SsoAdminClient
{
public class Class1
{
}
}

View File

@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<RootNamespace>VMware.vSphere.SsoAdminClient</RootNamespace>
<AssemblyName>VMware.vSphere.SsoAdminClient</AssemblyName>
<Description>SSO Admin API client.</Description>
<TargetFrameworks>net45;netcoreapp2.0</TargetFrameworks>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,147 @@
#**************************************************************************
# Copyright (c) VMware, Inc. All rights reserved.
#**************************************************************************
param(
[ValidateSet("Debug", "Release")]
[string]
$Configuration = "Release",
[string]
$TestVc,
[string]
$TestVcUser,
[string]
$TestVcPassword
)
function Test-BuildToolsAreAvailable {
$dotnetSdk = Get-Command 'dotnet'
if (-not $dotnetSdk) {
throw "'dotnet' sdk is not available"
}
}
function LogInfo($message) {
$dt = (Get-Date).ToLongTimeString()
Write-Host "[$dt] INFO: $message" -ForegroundColor Green
}
function Build {
$srcRoot = Join-Path $PSScriptRoot "VMware.vSphere.SsoAdmin.Client"
Push-Location $srcRoot
dotnet build -c $Configuration
Pop-Location
}
function Publish {
param($OutputFolder)
if (Test-Path $OutputFolder) {
$netcoreLsSource = [IO.Path]::Combine(
$PSScriptRoot,
"VMware.vSphere.SsoAdmin.Client",
"VMware.vSphere.LsClient",
"bin",
$Configuration,
"netcoreapp2.0",
"VMware.vSphere.LsClient.dll")
$net45LsSource = [IO.Path]::Combine(
$PSScriptRoot,
"VMware.vSphere.SsoAdmin.Client",
"VMware.vSphere.LsClient",
"bin",
$Configuration,
"netcoreapp2.0",
"VMware.vSphere.LsClient.dll")
$netcoreSsoAdminSource = [IO.Path]::Combine(
$PSScriptRoot,
"VMware.vSphere.SsoAdmin.Client",
"VMware.vSphere.SsoAdminClient",
"bin",
$Configuration,
"netcoreapp2.0",
"VMware.vSphere.SsoAdminClient.dll")
$net45SsoAdminSource = [IO.Path]::Combine(
$PSScriptRoot,
"VMware.vSphere.SsoAdmin.Client",
"VMware.vSphere.SsoAdminClient",
"bin",
$Configuration,
"net45",
"VMware.vSphere.SsoAdminClient.dll")
$netcoreUtilsSource = [IO.Path]::Combine(
$PSScriptRoot,
"VMware.vSphere.SsoAdmin.Client",
"VMware.vSphere.SsoAdmin.Utils",
"bin",
$Configuration,
"netcoreapp2.0",
"VMware.vSphere.SsoAdmin.Utils.dll")
$net45UtilsSource = [IO.Path]::Combine(
$PSScriptRoot,
"VMware.vSphere.SsoAdmin.Client",
"VMware.vSphere.SsoAdmin.Utils",
"bin",
$Configuration,
"net45",
"VMware.vSphere.SsoAdmin.Utils.dll")
$netcoreTarget = Join-Path $OutputFolder "netcoreapp2.0"
$net45Target = Join-Path $OutputFolder "net45"
Copy-Item -Path $netcoreLsSource -Destination $netcoreTarget -Force
Copy-Item -Path $net45LsSource -Destination $net45Target -Force
Copy-Item -Path $netcoreSsoAdminSource -Destination $netcoreTarget -Force
Copy-Item -Path $net45SsoAdminSource -Destination $net45Target -Force
Copy-Item -Path $netcoreUtilsSource -Destination $netcoreTarget -Force
Copy-Item -Path $net45UtilsSource -Destination $net45Target -Force
}
}
function Test {
if (-not [string]::IsNullOrEmpty($TestVc) -and `
-not [string]::IsNullOrEmpty($TestVcUser) -and `
-not [string]::IsNullOrEmpty($TestVcPassword)) {
# Run Tests in external process because it will load build output binaries
LogInfo "Run VC integration tests"
$usePowerShell = (Get-Process -Id $pid).ProcessName
$testLauncherScript = Join-Path (Join-Path $PSScriptRoot 'test') 'RunTests.ps1'
$arguments = "-Command $testLauncherScript -VcAddress $TestVc -VcUser $TestVcUser -VcUserPassword $TestVcPassword"
Start-Process `
-FilePath $usePowerShell `
-ArgumentList $arguments `
-PassThru `
-NoNewWindow | `
Wait-Process
}
}
# 1. Test Build Tools
LogInfo "Test build tools are available"
Test-BuildToolsAreAvailable
# 2. Build
LogInfo "Build"
Build
# 3. Publish
$OutputFolder = Split-Path $PSScriptRoot
LogInfo "Publish binaries to '$OutputFolder'"
Publish $OutputFolder
# 4. Test
Test

View File

@@ -0,0 +1,47 @@
#**************************************************************************
# Copyright (c) VMware, Inc. All rights reserved.
#**************************************************************************
param(
[Parameter(Mandatory = $true)]
[string]
$VcAddress,
[Parameter(Mandatory = $true)]
[string]
$VcUser,
[Parameter(Mandatory = $true)]
[string]
$VcUserPassword
)
# Import Vmware.vSphere.SsoAdmin Module
$modulePath = Join-Path (Split-Path $PSScriptRoot | Split-Path) "VMware.vSphere.SsoAdmin.psd1"
Import-Module $modulePath
$script:lsClient = $null
Describe "Lookup Service Client Integration Tests" {
Context "Retrieval of SsoAdmin API Url" {
BeforeAll {
## Create LsClient
$skipCertificateCheckValidator = New-Object `
'VMware.vSphere.SsoAdmin.Utils.AcceptAllX509CertificateValidator'
$script:lsClient = New-Object `
'VMware.vSphere.LsClient.LookupServiceClient' `
-ArgumentList @($VCAddress, $skipCertificateCheckValidator)
}
It 'Gets SsoAdmin API Url' {
# Act
$actual = $script:lsClient.GetSsoAdminEndpointUri()
# Assert
$actual | Should Not Be $null
$actual.ToString().StartsWith("https://$VCAddress/sso-adminserver/sdk/") | Should Be $true
}
}
}

View File

@@ -0,0 +1,38 @@
#**************************************************************************
# Copyright (c) VMware, Inc. All rights reserved.
#**************************************************************************
param(
[Parameter(Mandatory = $true)]
[string]
$VcAddress,
[Parameter(Mandatory = $true)]
[string]
$VcUser,
[Parameter(Mandatory = $true)]
[string]
$VcUserPassword
)
function Test-PesterIsAvailable() {
$pesterModule = Get-Module Pester -List
if ($pesterModule -eq $null) {
throw "Pester Module is not available"
}
}
Test-PesterIsAvailable
$testFiles = Get-ChildItem -Path $PSScriptRoot -Filter "*.Tests.ps1"
Invoke-Pester `
-Script @{
Path = $PSScriptRoot
Parameters = @{
VcAddress = $VcAddress
VcUser = $VcUser
VcUserPassword = $VcUserPassword
}
}