Merge pull request #180 from djshah19/user/dhwanishah/adding-proxy-support

User/dhwanishah/adding proxy support
This commit is contained in:
Dhwani Shah 2023-11-03 12:12:57 -07:00 коммит произвёл GitHub
Родитель fd0d275ce3 9d433fe604
Коммит bdb20508a2
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
6 изменённых файлов: 149 добавлений и 5 удалений

2
go.mod
Просмотреть файл

@ -7,7 +7,7 @@ require (
github.com/Azure/go-autorest/autorest v0.9.0
github.com/Azure/go-autorest/autorest/date v0.2.0
github.com/google/uuid v1.3.1
github.com/microsoft/moc v0.11.0-alpha.29
github.com/microsoft/moc v0.11.0-alpha.35
google.golang.org/grpc v1.58.2
k8s.io/klog v1.0.0
)

4
go.sum
Просмотреть файл

@ -1120,8 +1120,8 @@ github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/microsoft/moc v0.11.0-alpha.29 h1:SXqPMIXXdYlM5o3qlLU/cUf5kTByg/n8VWMMJ+Ls2bM=
github.com/microsoft/moc v0.11.0-alpha.29/go.mod h1:EuYNwYdC667rnJSYcLcLHKTuQURy9GLm7n+SMDhK6ps=
github.com/microsoft/moc v0.11.0-alpha.35 h1:VoP7AYmNbGTdE+rKQKltlYGe6ShD8fMa8/D3r7+ciSc=
github.com/microsoft/moc v0.11.0-alpha.35/go.mod h1:EuYNwYdC667rnJSYcLcLHKTuQURy9GLm7n+SMDhK6ps=
github.com/miekg/dns v1.1.25/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8/go.mod h1:mC1jAcsrzbxHt8iiaC+zU4b1ylILSosueou12R++wfY=
github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3/go.mod h1:RagcQ7I8IeTMnF8JTXieKnO4Z6JCsikNEzj0DwauVzE=

Просмотреть файл

@ -197,6 +197,8 @@ type OSProfile struct {
LinuxConfiguration *LinuxConfiguration `json:"linuxconfiguration,omitempty"`
// Bootstrap engine
OsBootstrapEngine OperatingSystemBootstrapEngine `json:"osbootstrapengine,omitempty"`
// ProxyConfiguration
ProxyConfiguration *ProxyConfiguration `json:"proxyconfiguration,omitempty"`
}
// VirtualMachineCustomSize Specifies cpu/memory information for custom VMSize types.
@ -1147,3 +1149,14 @@ type VirtualMachineRunCommandResponse struct {
// InstanceView - The virtual machine run command instance view.
InstanceView *VirtualMachineRunCommandInstanceView `json:"instanceView,omitempty"`
}
type ProxyConfiguration struct {
// The HTTP proxy server endpoint
HttpProxy *string `json:"httpproxy,omitempty"`
// The HTTPS proxy server endpoint
HttpsProxy *string `json:"httpsproxy,omitempty"`
// The endpoints that should not go through proxy
NoProxy *[]string `json:"noproxy,omitempty"`
// Alternative CA cert to use for connecting to proxy server
TrustedCa *string `json:"trustedca,omitempty"`
}

Просмотреть файл

@ -4,13 +4,13 @@
package virtualmachine
import (
"github.com/microsoft/moc-sdk-for-go/services/compute"
"github.com/microsoft/moc/pkg/convert"
"github.com/microsoft/moc/pkg/errors"
"github.com/microsoft/moc-sdk-for-go/services/compute"
"github.com/microsoft/moc/pkg/status"
wssdcloudcompute "github.com/microsoft/moc/rpc/cloudagent/compute"
wssdcloudproto "github.com/microsoft/moc/rpc/common"
wssdcommon "github.com/microsoft/moc/rpc/common"
)
@ -401,6 +401,9 @@ func (c *client) getWssdVirtualMachineOSConfiguration(s *compute.OSProfile) (*ws
if s.CustomData != nil {
osconfig.CustomData = *s.CustomData
}
osconfig.ProxyConfiguration = c.getWssdVirtualMachineProxyConfiguration(s.ProxyConfiguration)
return &osconfig, nil
}
@ -418,6 +421,32 @@ func (c *client) getWssdVirtualMachineGuestAgentConfiguration(s *compute.GuestAg
return gac, nil
}
func (c *client) getWssdVirtualMachineProxyConfiguration(proxyConfig *compute.ProxyConfiguration) *wssdcloudproto.ProxyConfiguration {
if proxyConfig == nil {
return nil
}
proxyConfiguration := &wssdcloudproto.ProxyConfiguration{}
if proxyConfig.TrustedCa != nil {
proxyConfiguration.TrustedCa = *proxyConfig.TrustedCa
}
if proxyConfig.HttpProxy != nil {
proxyConfiguration.HttpProxy = *proxyConfig.HttpProxy
}
if proxyConfig.HttpsProxy != nil {
proxyConfiguration.HttpsProxy = *proxyConfig.HttpsProxy
}
if proxyConfig.NoProxy != nil {
proxyConfiguration.NoProxy = *proxyConfig.NoProxy
}
return proxyConfiguration
}
// Conversion functions from wssdcloudcompute to compute
func (c *client) getVirtualMachine(vm *wssdcloudcompute.VirtualMachine, group string) *compute.VirtualMachine {
@ -695,5 +724,19 @@ func (c *client) getVirtualMachineOSProfile(o *wssdcloudcompute.OperatingSystemC
OsBootstrapEngine: osBootstrapEngine,
WindowsConfiguration: c.getVirtualMachineWindowsConfiguration(o.WindowsConfiguration),
LinuxConfiguration: c.getVirtualMachineLinuxConfiguration(o.LinuxConfiguration),
ProxyConfiguration: c.getVirtualMachineProxyConfiguration(o.ProxyConfiguration),
}
}
func (c *client) getVirtualMachineProxyConfiguration(proxyConfiguration *wssdcloudproto.ProxyConfiguration) *compute.ProxyConfiguration {
if proxyConfiguration == nil {
return nil
}
return &compute.ProxyConfiguration{
HttpProxy: &proxyConfiguration.HttpProxy,
HttpsProxy: &proxyConfiguration.HttpsProxy,
NoProxy: &proxyConfiguration.NoProxy,
TrustedCa: &proxyConfiguration.TrustedCa,
}
}

Просмотреть файл

@ -4,7 +4,12 @@
package virtualmachine
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/microsoft/moc-sdk-for-go/services/compute"
"github.com/microsoft/moc/pkg/certs"
)
func Test_getWssdVirtualMachine(t *testing.T) {
@ -27,3 +32,60 @@ func Test_getVirtualMachineStorageProfileOsDisk(t *testing.T) {}
func Test_getVirtualMachineStorageProfileDataDisks(t *testing.T) {}
func Test_getVirtualMachineNetworkProfile(t *testing.T) {}
func Test_getVirtualMachineOSProfile(t *testing.T) {}
func Test_getWssdVirtualMachineProxyConfiguration(t *testing.T) {
proxy := NewProxy()
defer proxy.Target.Close()
HttpProxy := proxy.Target.URL
HttpsProxy := proxy.Target.URL
NoProxy := []string{"localhost", "127.0.0.1", ".svc", "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "100.0.0.0/8", ".corp.microsoft.com", ".masd.stbtest.microsoft.com"}
caCert, _, err := certs.GenerateClientCertificate("ValidCertificate")
if err != nil {
t.Fatalf(err.Error())
}
certBytes := certs.EncodeCertPEM(caCert)
TrustedCa := string(certBytes)
proxyConfig := &compute.ProxyConfiguration{
HttpProxy: &HttpProxy,
HttpsProxy: &HttpsProxy,
NoProxy: &NoProxy,
TrustedCa: &TrustedCa,
}
wssdcloudclient := client{}
config := wssdcloudclient.getWssdVirtualMachineProxyConfiguration(proxyConfig)
if config.HttpProxy != HttpProxy {
t.Fatalf("Test_getWssdVirtualMachineProxyConfiguration test case failed: HttpProxy does not match")
}
if config.HttpsProxy != HttpsProxy {
t.Fatalf("Test_getWssdVirtualMachineProxyConfiguration test case failed: HttpsProxy does not match")
}
if len(config.NoProxy) != len(NoProxy) {
t.Fatalf("Test_getWssdVirtualMachineProxyConfiguration test case failed: NoProxy does not match")
}
if config.TrustedCa != TrustedCa {
t.Fatalf("Test_getWssdVirtualMachineProxyConfiguration test case failed: TrustedCa does not match")
}
config = wssdcloudclient.getWssdVirtualMachineProxyConfiguration(nil)
if config != nil && err != nil {
t.Fatalf("Test_getWssdVirtualMachineProxyConfiguration test case failed: Expected output to be nil since input passed was nil")
}
}
// Proxy is a simple proxy server for unit tests.
type Proxy struct {
Target *httptest.Server
}
// NewProxy creates a new proxy server for unit tests.
func NewProxy() *Proxy {
target := httptest.NewServer(http.DefaultServeMux)
return &Proxy{Target: target}
}

Просмотреть файл

@ -13,6 +13,7 @@ import (
"github.com/microsoft/moc/pkg/errors"
"github.com/microsoft/moc/pkg/marshal"
prototags "github.com/microsoft/moc/pkg/tags"
"github.com/microsoft/moc/pkg/validations"
wssdcloudproto "github.com/microsoft/moc/rpc/common"
wssdcloudclient "github.com/microsoft/moc-sdk-for-go/pkg/client"
@ -273,6 +274,10 @@ func (c *client) getVirtualMachineRequest(opType wssdcloudproto.Operation, group
GroupName: group,
}
if vmss != nil {
err = c.virtualMachineValidations(opType, vmss)
if err != nil {
return nil, err
}
wssdvm, err = c.getWssdVirtualMachine(vmss, group)
if err != nil {
return nil, err
@ -305,3 +310,24 @@ func getComputeTags(tags *wssdcloudproto.Tags) map[string]*string {
func getWssdTags(tags map[string]*string) *wssdcloudproto.Tags {
return prototags.MapToProto(tags)
}
func (c *client) virtualMachineValidations(opType wssdcloudproto.Operation, vmss *compute.VirtualMachine) error {
if vmss.OsProfile == nil {
return nil
}
if vmss.OsProfile.ProxyConfiguration != nil && opType == wssdcloudproto.Operation_POST {
if vmss.OsProfile.ProxyConfiguration.HttpProxy != nil {
_, err := validations.ValidateProxyURL(*vmss.OsProfile.ProxyConfiguration.HttpProxy)
if err != nil {
return errors.Wrapf(errors.InvalidInput, err.Error())
}
}
if vmss.OsProfile.ProxyConfiguration.HttpsProxy != nil {
_, err := validations.ValidateProxyURL(*vmss.OsProfile.ProxyConfiguration.HttpsProxy)
if err != nil {
return errors.Wrapf(errors.InvalidInput, err.Error())
}
}
}
return nil
}