diff --git a/cli/pkg/serviceLib/flags/flags.go b/cli/pkg/serviceLib/flags/flags.go index d1576b3b..d87c6a15 100644 --- a/cli/pkg/serviceLib/flags/flags.go +++ b/cli/pkg/serviceLib/flags/flags.go @@ -2,6 +2,12 @@ package flags +// Constants +const ( + PROTOCOL_HTTP = "http" + PROTOCOL_HTTPS = "https" +) + // CLI flag component descriptor const ( SERVICE string = "service" @@ -131,26 +137,26 @@ const ( SERVICE_NET_IP_DFLT string = "127.0.0.1" SERVICE_NET_PORT_DFLT uint16 = 8080 SERVICE_INSECURE_DFLT bool = false - SERVICE_PROTOCOL_DFLT string = "http" + SERVICE_PROTOCOL_DFLT string = PROTOCOL_HTTPS APPLIANCE_NET_IP_DFLT string = "127.0.0.1" APPLIANCE_NET_PORT_DFLT uint16 = 443 APPLIANCE_INSECURE_DFLT bool = false - APPLIANCE_PROTOCOL_DFLT string = "https" + APPLIANCE_PROTOCOL_DFLT string = PROTOCOL_HTTPS APPLIANCE_USERNAME_DFLT string = "dummyuser" APPLIANCE_PASSWORD_DFLT string = "dummypswd" BLADE_NET_IP_DFLT string = "127.0.0.1" BLADE_NET_PORT_DFLT uint16 = 443 BLADE_INSECURE_DFLT bool = false - BLADE_PROTOCOL_DFLT string = "https" + BLADE_PROTOCOL_DFLT string = PROTOCOL_HTTPS BLADE_USERNAME_DFLT string = "root" BLADE_PASSWORD_DFLT string = "0penBmc" HOST_NET_IP_DFLT string = "127.0.0.1" HOST_NET_PORT_DFLT uint16 = 8082 HOST_INSECURE_DFLT bool = false - HOST_PROTOCOL_DFLT string = "http" + HOST_PROTOCOL_DFLT string = PROTOCOL_HTTP HOST_USERNAME_DFLT string = "admin" HOST_PASSWORD_DFLT string = "admin12345" diff --git a/cli/pkg/serviceLib/serviceRequests/appliances.go b/cli/pkg/serviceLib/serviceRequests/appliances.go index 57f32285..01b4c73c 100644 --- a/cli/pkg/serviceLib/serviceRequests/appliances.go +++ b/cli/pkg/serviceLib/serviceRequests/appliances.go @@ -39,7 +39,7 @@ func (r *ServiceRequestAddAppliance) Execute() (*service.Appliance, error) { klog.V(4).InfoS(fmt.Sprintf("%T", *r), "AppliancesCred", fmt.Sprintf("%+v", *r.ApplianceCred)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ApplianceTcp", fmt.Sprintf("%+v", *r.ApplianceTcp)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort()) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) insecure := r.ApplianceTcp.GetInsecure() protocol := r.ApplianceTcp.GetProtocol() @@ -79,7 +79,7 @@ func (r *ServiceRequestDeleteAppliance) Execute() (*service.Appliance, error) { klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ServiceTcp", fmt.Sprintf("%+v", *r.ServiceTcp)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ApplianceId", fmt.Sprintf("%+v", *r.ApplianceId)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort()) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) deletedAppliance, err := serviceWrap.DeleteApplianceById(serviceClient, r.ApplianceId.GetId()) if err != nil { @@ -102,7 +102,7 @@ func NewServiceRequestListAppliances(cmd *cobra.Command) *ServiceRequestListAppl func (r *ServiceRequestListAppliances) Execute() (*[]*service.Appliance, error) { klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ServiceTcp", fmt.Sprintf("%+v", *r.ServiceTcp)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.ip, r.ServiceTcp.port) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) appliances, err := serviceWrap.GetAllAppliances(serviceClient) if err != nil { @@ -147,7 +147,7 @@ func (r *ServiceRequestRenameAppliance) Execute() (*service.Appliance, error) { klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ApplianceId", fmt.Sprintf("%+v", *r.ApplianceId)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "NewApplianceId", fmt.Sprintf("%+v", *r.NewApplianceId)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort()) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) appliance, err := serviceWrap.RenameApplianceById(serviceClient, r.ApplianceId.GetId(), r.NewApplianceId.GetId()) if err != nil { @@ -173,7 +173,7 @@ func (r *ServiceRequestResyncAppliance) Execute() (*service.Appliance, error) { klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ServiceTcp", fmt.Sprintf("%+v", *r.ServiceTcp)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ApplianceId", fmt.Sprintf("%+v", *r.ApplianceId)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort()) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) appliance, err := serviceWrap.ResyncApplianceById(serviceClient, r.ApplianceId.GetId()) if err != nil { diff --git a/cli/pkg/serviceLib/serviceRequests/blades.go b/cli/pkg/serviceLib/serviceRequests/blades.go index 5591a1e3..c5a20ec6 100644 --- a/cli/pkg/serviceLib/serviceRequests/blades.go +++ b/cli/pkg/serviceLib/serviceRequests/blades.go @@ -39,7 +39,7 @@ func (r *ServiceRequestAddBlade) Execute() (*service.Blade, error) { klog.V(4).InfoS(fmt.Sprintf("%T", *r), "BladeCred", fmt.Sprintf("%+v", *r.BladeCred)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "BladeTcp", fmt.Sprintf("%+v", *r.BladeTcp)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort()) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) insecure := r.BladeTcp.GetInsecure() protocol := r.BladeTcp.GetProtocol() @@ -82,7 +82,7 @@ func (r *ServiceRequestDeleteBlade) Execute() (*service.Blade, error) { klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ApplianceId", fmt.Sprintf("%+v", *r.ApplianceId)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "BladeId", fmt.Sprintf("%+v", *r.BladeId)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort()) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) blade, err := serviceWrap.DeleteBladeById(serviceClient, r.ApplianceId.GetId(), r.BladeId.GetId()) if err != nil { @@ -123,7 +123,7 @@ func (r *ServiceRequestListBlades) Execute() (*serviceWrap.ApplianceBladeSummary klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ServiceTcp", fmt.Sprintf("%+v", *r.ServiceTcp)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ApplianceId", fmt.Sprintf("%+v", *r.ApplianceId)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort()) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) if !r.AllAppliances() && !r.AllBlades() { blade, err := serviceWrap.FindBladeById_SingleAppl(serviceClient, r.ApplianceId.GetId(), r.BladeId.GetId()) @@ -201,7 +201,7 @@ func (r *ServiceRequestRenameBlade) Execute() (*service.Blade, error) { klog.V(4).InfoS(fmt.Sprintf("%T", *r), "BladeId", fmt.Sprintf("%+v", *r.BladeId)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "NewBladeId", fmt.Sprintf("%+v", *r.NewBladeId)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort()) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) blade, err := serviceWrap.RenameBladeById(serviceClient, r.ApplianceId.GetId(), r.BladeId.GetId(), r.NewBladeId.GetId()) if err != nil { @@ -230,7 +230,7 @@ func (r *ServiceRequestResyncBlade) Execute() (*service.Blade, error) { klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ApplianceId", fmt.Sprintf("%+v", *r.ApplianceId)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "BladeId", fmt.Sprintf("%+v", *r.BladeId)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort()) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) blade, err := serviceWrap.ResyncBladeById(serviceClient, r.ApplianceId.GetId(), r.BladeId.GetId()) if err != nil { diff --git a/cli/pkg/serviceLib/serviceRequests/hosts.go b/cli/pkg/serviceLib/serviceRequests/hosts.go index c3e41d4a..1ca9cdbc 100644 --- a/cli/pkg/serviceLib/serviceRequests/hosts.go +++ b/cli/pkg/serviceLib/serviceRequests/hosts.go @@ -36,7 +36,7 @@ func (r *ServiceRequestAddHost) Execute() (*service.Host, error) { klog.V(4).InfoS(fmt.Sprintf("%T", *r), "HostCred", fmt.Sprintf("%+v", *r.HostCred)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "HostTcp", fmt.Sprintf("%+v", *r.HostTcp)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort()) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) insecure := r.HostTcp.GetInsecure() protocol := r.HostTcp.GetProtocol() @@ -79,7 +79,7 @@ func (r *ServiceRequestDeleteHost) Execute() (*service.Host, error) { klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ServiceTcp", fmt.Sprintf("%+v", *r.ServiceTcp)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "HostId", fmt.Sprintf("%+v", *r.HostId)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort()) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) host, err = serviceWrap.DeleteHostById(serviceClient, r.HostId.GetId()) if err != nil { @@ -102,7 +102,7 @@ func NewServiceRequestListHosts(cmd *cobra.Command) *ServiceRequestListHosts { func (r *ServiceRequestListHosts) Execute() (*[]*service.Host, error) { klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ServiceTcp", fmt.Sprintf("%+v", *r.ServiceTcp)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.ip, r.ServiceTcp.port) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) hosts, err := serviceWrap.GetAllHosts(serviceClient) if err != nil { @@ -147,7 +147,7 @@ func (r *ServiceRequestRenameHost) Execute() (*service.Host, error) { klog.V(4).InfoS(fmt.Sprintf("%T", *r), "HostId", fmt.Sprintf("%+v", *r.HostId)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "NewHostId", fmt.Sprintf("%+v", *r.NewHostId)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort()) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) host, err := serviceWrap.RenameHostById(serviceClient, r.HostId.GetId(), r.NewHostId.GetId()) if err != nil { @@ -173,7 +173,7 @@ func (r *ServiceRequestResyncHost) Execute() (*service.Host, error) { klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ServiceTcp", fmt.Sprintf("%+v", *r.ServiceTcp)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "HostId", fmt.Sprintf("%+v", *r.HostId)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort()) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) host, err := serviceWrap.ResyncHostById(serviceClient, r.HostId.GetId()) if err != nil { diff --git a/cli/pkg/serviceLib/serviceRequests/memory-devices.go b/cli/pkg/serviceLib/serviceRequests/memory-devices.go index cf23fae1..07c204ef 100644 --- a/cli/pkg/serviceLib/serviceRequests/memory-devices.go +++ b/cli/pkg/serviceLib/serviceRequests/memory-devices.go @@ -45,7 +45,7 @@ func (r *ServiceRequestListHostMemoryDevices) Execute() (*serviceWrap.HostMemory klog.V(4).InfoS(fmt.Sprintf("%T", *r), "HostId", fmt.Sprintf("%+v", *r.HostId)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "MemoryDeviceId", fmt.Sprintf("%+v", *r.MemoryDeviceId)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.ip, r.ServiceTcp.port) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) if r.AllHosts() && r.AllMemoryDevices() { summary, err = serviceWrap.GetMemoryDevices_AllHosts(serviceClient) diff --git a/cli/pkg/serviceLib/serviceRequests/memory.go b/cli/pkg/serviceLib/serviceRequests/memory.go index 747c5a87..a7b391eb 100644 --- a/cli/pkg/serviceLib/serviceRequests/memory.go +++ b/cli/pkg/serviceLib/serviceRequests/memory.go @@ -56,7 +56,7 @@ func (r *ServiceRequestListMemoryRegions) Execute() (*serviceWrap.BladeMemoryReg klog.V(4).InfoS(fmt.Sprintf("%T", *r), "BladeId", fmt.Sprintf("%+v", *r.BladeId)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "MemoryId", fmt.Sprintf("%+v", *r.MemoryId)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.ip, r.ServiceTcp.port) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) if r.AllAppliances() && r.AllBlades() && r.AllMemoryRegions() { summary, err = serviceWrap.GetMemoryRegions_AllApplsAllBlades(serviceClient) @@ -151,42 +151,16 @@ func (r *ServiceRequestListMemoryRegions) OutputSummaryListMemory(s *serviceWrap } type ServiceRequestComposeMemory struct { - serviceTcp *TcpInfo - applianceId *Id - bladeId *Id - portId *Id - resourceSize *Size - qos int32 -} - -//TODO: Should I propogate: private struct variables, forcing Getter usage -and- adding pointer safety to all Getter's (like generated cfm-service client) - -func (r *ServiceRequestComposeMemory) GetServiceIp() string { - return r.serviceTcp.GetIp() -} - -func (r *ServiceRequestComposeMemory) GetServicePort() uint16 { - return r.serviceTcp.GetPort() -} - -func (r *ServiceRequestComposeMemory) GetApplianceId() string { - return r.applianceId.GetId() -} - -func (r *ServiceRequestComposeMemory) GetBladeId() string { - return r.bladeId.GetId() -} - -func (r *ServiceRequestComposeMemory) GetPortId() string { - return r.portId.GetId() -} - -func (r *ServiceRequestComposeMemory) GetResourceSizeGiB() int32 { - return r.resourceSize.GetSizeGiB() + ServiceTcp *TcpInfo + ApplianceId *Id + BladeId *Id + PortId *Id + ResourceSize *Size + Qos int32 } func (r *ServiceRequestComposeMemory) GetQos() int32 { - return r.qos + return r.Qos } func NewServiceRequestComposeMemory(cmd *cobra.Command) *ServiceRequestComposeMemory { @@ -198,25 +172,25 @@ func NewServiceRequestComposeMemory(cmd *cobra.Command) *ServiceRequestComposeMe } return &ServiceRequestComposeMemory{ - serviceTcp: NewTcpInfo(cmd, flags.SERVICE), - applianceId: NewId(cmd, flags.APPLIANCE), - bladeId: NewId(cmd, flags.BLADE), - portId: NewId(cmd, flags.PORT), - resourceSize: NewSize(cmd, flags.RESOURCE), - qos: qos, + ServiceTcp: NewTcpInfo(cmd, flags.SERVICE), + ApplianceId: NewId(cmd, flags.APPLIANCE), + BladeId: NewId(cmd, flags.BLADE), + PortId: NewId(cmd, flags.PORT), + ResourceSize: NewSize(cmd, flags.RESOURCE), + Qos: qos, } } func (r *ServiceRequestComposeMemory) Execute() (*service.MemoryRegion, error) { - klog.V(4).InfoS(fmt.Sprintf("%T", *r), "serviceTcp", fmt.Sprintf("%+v", *r.serviceTcp)) - klog.V(4).InfoS(fmt.Sprintf("%T", *r), "applianceId", fmt.Sprintf("%+v", *r.applianceId)) - klog.V(4).InfoS(fmt.Sprintf("%T", *r), "bladeId", fmt.Sprintf("%+v", *r.bladeId)) - klog.V(4).InfoS(fmt.Sprintf("%T", *r), "portId", fmt.Sprintf("%+v", *r.portId)) - klog.V(4).InfoS(fmt.Sprintf("%T", *r), "resourceSize", fmt.Sprintf("%+v", r.resourceSize)) + klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ServiceTcp", fmt.Sprintf("%+v", *r.ServiceTcp)) + klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ApplianceId", fmt.Sprintf("%+v", *r.ApplianceId)) + klog.V(4).InfoS(fmt.Sprintf("%T", *r), "BladeId", fmt.Sprintf("%+v", *r.BladeId)) + klog.V(4).InfoS(fmt.Sprintf("%T", *r), "PortId", fmt.Sprintf("%+v", *r.PortId)) + klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ResourceSize", fmt.Sprintf("%+v", r.ResourceSize)) - serviceClient := serviceWrap.GetServiceClient(r.GetServiceIp(), r.GetServicePort()) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) - region, err := serviceWrap.ComposeMemory(serviceClient, r.GetApplianceId(), r.GetBladeId(), r.GetPortId(), r.GetResourceSizeGiB()*1024, r.GetQos()) + region, err := serviceWrap.ComposeMemory(serviceClient, r.ApplianceId.GetId(), r.BladeId.GetId(), r.PortId.GetId(), r.ResourceSize.GetSizeGiB()*1024, r.GetQos()) if err != nil { return nil, fmt.Errorf("failure: compose memory: %s", err) } @@ -235,51 +209,31 @@ func (r *ServiceRequestComposeMemory) OutputResultsComposedMemory(m *service.Mem } type ServiceRequestFreeMemory struct { - serviceTcp *TcpInfo - applianceId *Id - bladeId *Id - memoryId *Id -} - -func (r *ServiceRequestFreeMemory) GetServiceIp() string { - return r.serviceTcp.GetIp() -} - -func (r *ServiceRequestFreeMemory) GetServicePort() uint16 { - return r.serviceTcp.GetPort() -} - -func (r *ServiceRequestFreeMemory) GetApplianceId() string { - return r.applianceId.GetId() -} - -func (r *ServiceRequestFreeMemory) GetBladeId() string { - return r.bladeId.GetId() -} - -func (r *ServiceRequestFreeMemory) GetMemoryId() string { - return r.memoryId.GetId() + ServiceTcp *TcpInfo + ApplianceId *Id + BladeId *Id + MemoryId *Id } func NewServiceRequestFreeMemory(cmd *cobra.Command) *ServiceRequestFreeMemory { return &ServiceRequestFreeMemory{ - serviceTcp: NewTcpInfo(cmd, flags.SERVICE), - applianceId: NewId(cmd, flags.APPLIANCE), - bladeId: NewId(cmd, flags.BLADE), - memoryId: NewId(cmd, flags.MEMORY), + ServiceTcp: NewTcpInfo(cmd, flags.SERVICE), + ApplianceId: NewId(cmd, flags.APPLIANCE), + BladeId: NewId(cmd, flags.BLADE), + MemoryId: NewId(cmd, flags.MEMORY), } } func (r *ServiceRequestFreeMemory) Execute() (*service.MemoryRegion, error) { - klog.V(4).InfoS(fmt.Sprintf("%T", *r), "serviceTcp", fmt.Sprintf("%+v", *r.serviceTcp)) - klog.V(4).InfoS(fmt.Sprintf("%T", *r), "applianceId", fmt.Sprintf("%+v", *r.applianceId)) - klog.V(4).InfoS(fmt.Sprintf("%T", *r), "bladeId", fmt.Sprintf("%+v", *r.bladeId)) - klog.V(4).InfoS(fmt.Sprintf("%T", *r), "memoryId", fmt.Sprintf("%+v", *r.memoryId)) + klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ServiceTcp", fmt.Sprintf("%+v", *r.ServiceTcp)) + klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ApplianceId", fmt.Sprintf("%+v", *r.ApplianceId)) + klog.V(4).InfoS(fmt.Sprintf("%T", *r), "BladeId", fmt.Sprintf("%+v", *r.BladeId)) + klog.V(4).InfoS(fmt.Sprintf("%T", *r), "MemoryId", fmt.Sprintf("%+v", *r.MemoryId)) - serviceClient := serviceWrap.GetServiceClient(r.GetServiceIp(), r.GetServicePort()) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) - region, err := serviceWrap.FreeMemory(serviceClient, r.GetApplianceId(), r.GetBladeId(), r.GetMemoryId()) + region, err := serviceWrap.FreeMemory(serviceClient, r.ApplianceId.GetId(), r.BladeId.GetId(), r.MemoryId.GetId()) if err != nil { return nil, fmt.Errorf("failure: free memory: %s", err) } @@ -330,7 +284,7 @@ func (r *ServiceRequestListHostMemoryRegions) Execute() (*serviceWrap.HostMemory klog.V(4).InfoS(fmt.Sprintf("%T", *r), "HostId", fmt.Sprintf("%+v", *r.HostId)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "MemoryId", fmt.Sprintf("%+v", *r.MemoryId)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.ip, r.ServiceTcp.port) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) if r.AllHosts() && r.AllMemoryRegions() { summary, err = serviceWrap.GetMemoryRegions_AllHosts(serviceClient) @@ -401,3 +355,68 @@ func (r *ServiceRequestListHostMemoryRegions) OutputSummaryListMemory(s *service fmt.Printf("\n") } + +// ServiceRequestBladesAssignMemory - This request structure supports BOTH "assign" and "unassign" of a memory region to\from a port +// This is an artifact of the way the cfm-service client api is setup. +type ServiceRequestBladesAssignMemory struct { + ServiceTcp *TcpInfo + ApplianceId *Id + BladeId *Id + MemoryId *Id + PortId *Id + Operation string //"assign" or "unassign" +} + +func NewServiceRequestBladesAssignMemory(cmd *cobra.Command, operation string) *ServiceRequestBladesAssignMemory { + + if operation != "assign" && operation != "unassign" { + newErr := fmt.Errorf("failure: NewServiceRequestBladesAssignMemory: operation options: 'assign', 'unassign'") + klog.ErrorS(newErr, "Invalid parameter value", "operation", operation) + cobra.CheckErr(newErr) + } + + return &ServiceRequestBladesAssignMemory{ + ServiceTcp: NewTcpInfo(cmd, flags.SERVICE), + ApplianceId: NewId(cmd, flags.APPLIANCE), + BladeId: NewId(cmd, flags.BLADE), + MemoryId: NewId(cmd, flags.MEMORY), + PortId: NewId(cmd, flags.PORT), + Operation: operation, + } +} + +func (r *ServiceRequestBladesAssignMemory) GetOperation() string { + return r.Operation +} + +func (r *ServiceRequestBladesAssignMemory) Execute() (*service.MemoryRegion, error) { + var err error + + klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ServiceTcp", fmt.Sprintf("%+v", *r.ServiceTcp)) + klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ApplianceId", fmt.Sprintf("%+v", *r.ApplianceId)) + klog.V(4).InfoS(fmt.Sprintf("%T", *r), "BladeId", fmt.Sprintf("%+v", *r.BladeId)) + klog.V(4).InfoS(fmt.Sprintf("%T", *r), "MemoryId", fmt.Sprintf("%+v", *r.MemoryId)) + klog.V(4).InfoS(fmt.Sprintf("%T", *r), "PortId", fmt.Sprintf("%+v", *r.PortId)) + klog.V(4).InfoS(fmt.Sprintf("%T", *r), "Operation", r.Operation) + + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) + + region, err := serviceWrap.BladesAssignMemory(serviceClient, r.ApplianceId.GetId(), r.BladeId.GetId(), r.MemoryId.GetId(), r.PortId.GetId(), r.GetOperation()) + if err != nil { + newErr := fmt.Errorf("failure: assign blade memory region to blade port: %w", err) + klog.ErrorS(newErr, "Execute failure", "memoryId", r.MemoryId.GetId(), "portId", r.PortId.GetId(), "applId", r.ApplianceId.GetId(), "bladeId", r.BladeId.GetId()) + return nil, newErr + } + + return region, nil +} + +func (r *ServiceRequestBladesAssignMemory) OutputSummaryBladesAssignMemory(m *service.MemoryRegion) { + fmt.Printf("\n%s Memory and Port Summary\n", strings.ToUpper(r.GetOperation())) + fmt.Printf("Status: %s\n\n", m.GetStatus()) + fmt.Printf("%-15s %-15s %-15s %-25s\n", "Memory ID", "Port ID", "Blade ID", "Appliance ID") + fmt.Printf("%s %s %s %s\n", strings.Repeat("-", 15), strings.Repeat("-", 15), strings.Repeat("-", 15), strings.Repeat("-", 25)) + fmt.Printf("%-15s %-15s %-15s %-25s\n", m.GetId(), m.GetMemoryAppliancePort(), m.GetMemoryBladeId(), m.GetMemoryApplianceId()) + + fmt.Printf("\n") +} diff --git a/cli/pkg/serviceLib/serviceRequests/memoryBlade.go b/cli/pkg/serviceLib/serviceRequests/memoryBlade.go deleted file mode 100644 index e7f6e123..00000000 --- a/cli/pkg/serviceLib/serviceRequests/memoryBlade.go +++ /dev/null @@ -1,104 +0,0 @@ -// Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates - -package serviceRequests - -import ( - "cfm/cli/pkg/serviceLib/flags" - "cfm/cli/pkg/serviceLib/serviceWrap" - "fmt" - "strings" - - service "cfm/pkg/client" - - "github.com/spf13/cobra" - "k8s.io/klog/v2" -) - -// ServiceRequestBladesAssignMemory - This request structure supports BOTH "assign" and "unassign" of a memory region to\from a port -// This is an artifact of the way the cfm-service client api is setup. -type ServiceRequestBladesAssignMemory struct { - serviceTcp *TcpInfo - applianceId *Id - bladeId *Id - memoryId *Id - portId *Id - operation string //"assign" or "unassign" -} - -func NewServiceRequestBladesAssignMemory(cmd *cobra.Command, operation string) *ServiceRequestBladesAssignMemory { - - if operation != "assign" && operation != "unassign" { - newErr := fmt.Errorf("Error: NewServiceRequestBladesAssignMemory: operation options: 'assign', 'unassign'") - klog.ErrorS(newErr, "Invalid parameter value", "operation", operation) - cobra.CheckErr(newErr) - } - - return &ServiceRequestBladesAssignMemory{ - serviceTcp: NewTcpInfo(cmd, flags.SERVICE), - applianceId: NewId(cmd, flags.APPLIANCE), - bladeId: NewId(cmd, flags.BLADE), - memoryId: NewId(cmd, flags.MEMORY), - portId: NewId(cmd, flags.PORT), - operation: operation, - } -} - -func (r *ServiceRequestBladesAssignMemory) GetServiceIp() string { - return r.serviceTcp.GetIp() -} - -func (r *ServiceRequestBladesAssignMemory) GetServicePort() uint16 { - return r.serviceTcp.GetPort() -} - -func (r *ServiceRequestBladesAssignMemory) GetApplianceId() string { - return r.applianceId.GetId() -} - -func (r *ServiceRequestBladesAssignMemory) GetBladeId() string { - return r.bladeId.GetId() -} - -func (r *ServiceRequestBladesAssignMemory) GetMemoryId() string { - return r.memoryId.GetId() -} - -func (r *ServiceRequestBladesAssignMemory) GetPortId() string { - return r.portId.GetId() -} - -func (r *ServiceRequestBladesAssignMemory) GetOperation() string { - return r.operation -} - -func (r *ServiceRequestBladesAssignMemory) Execute() (*service.MemoryRegion, error) { - var err error - - klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ServiceTcp", fmt.Sprintf("%+v", *r.serviceTcp)) - klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ApplianceId", fmt.Sprintf("%+v", *r.applianceId)) - klog.V(4).InfoS(fmt.Sprintf("%T", *r), "BladeId", fmt.Sprintf("%+v", *r.bladeId)) - klog.V(4).InfoS(fmt.Sprintf("%T", *r), "MemoryId", fmt.Sprintf("%+v", *r.memoryId)) - klog.V(4).InfoS(fmt.Sprintf("%T", *r), "PortId", fmt.Sprintf("%+v", *r.portId)) - klog.V(4).InfoS(fmt.Sprintf("%T", *r), "Operation", r.operation) - - serviceClient := serviceWrap.GetServiceClient(r.GetServiceIp(), r.GetServicePort()) - - region, err := serviceWrap.BladesAssignMemory(serviceClient, r.GetApplianceId(), r.GetBladeId(), r.GetMemoryId(), r.GetPortId(), r.GetOperation()) - if err != nil { - newErr := fmt.Errorf("failure: assign blade memory region to blade port: %w", err) - klog.ErrorS(newErr, "Execute failure", "memoryId", r.GetMemoryId(), "portId", r.GetPortId(), "applId", r.GetApplianceId(), "bladeId", r.GetBladeId()) - return nil, newErr - } - - return region, nil -} - -func (r *ServiceRequestBladesAssignMemory) OutputSummaryBladesAssignMemory(m *service.MemoryRegion) { - fmt.Printf("\n%s Memory and Port Summary\n", strings.ToUpper(r.GetOperation())) - fmt.Printf("Status: %s\n\n", m.GetStatus()) - fmt.Printf("%-15s %-15s %-15s %-25s\n", "Memory ID", "Port ID", "Blade ID", "Appliance ID") - fmt.Printf("%s %s %s %s\n", strings.Repeat("-", 15), strings.Repeat("-", 15), strings.Repeat("-", 15), strings.Repeat("-", 25)) - fmt.Printf("%-15s %-15s %-15s %-25s\n", m.GetId(), m.GetMemoryAppliancePort(), m.GetMemoryBladeId(), m.GetMemoryApplianceId()) - - fmt.Printf("\n") -} diff --git a/cli/pkg/serviceLib/serviceRequests/ports.go b/cli/pkg/serviceLib/serviceRequests/ports.go index fd7407f2..ccb552ff 100644 --- a/cli/pkg/serviceLib/serviceRequests/ports.go +++ b/cli/pkg/serviceLib/serviceRequests/ports.go @@ -53,7 +53,7 @@ func (r *ServiceRequestListBladePorts) Execute() (*serviceWrap.BladePortsSummary klog.V(4).InfoS(fmt.Sprintf("%T", *r), "BladeId", fmt.Sprintf("%+v", *r.BladeId)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "PortId", fmt.Sprintf("%+v", *r.PortId)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.ip, r.ServiceTcp.port) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) if r.AllAppliances() && r.AllBlades() && r.AllPorts() { summary, err = serviceWrap.GetPorts_AllApplsAllBlades(serviceClient) @@ -179,7 +179,7 @@ func (r *ServiceRequestListHostPorts) Execute() (*serviceWrap.HostPortSummary, e klog.V(4).InfoS(fmt.Sprintf("%T", *r), "HostId", fmt.Sprintf("%+v", *r.HostId)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "PortId", fmt.Sprintf("%+v", *r.PortId)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.ip, r.ServiceTcp.port) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) if r.AllHosts() && r.AllPorts() { summary, err = serviceWrap.GetAllPorts_AllHosts(serviceClient) diff --git a/cli/pkg/serviceLib/serviceRequests/resources.go b/cli/pkg/serviceLib/serviceRequests/resources.go index 8067c46b..d205ec06 100644 --- a/cli/pkg/serviceLib/serviceRequests/resources.go +++ b/cli/pkg/serviceLib/serviceRequests/resources.go @@ -56,7 +56,7 @@ func (r *ServiceRequestListResources) Execute() (*serviceWrap.ResourceBlockSumma klog.V(4).InfoS(fmt.Sprintf("%T", *r), "BladeId", fmt.Sprintf("%+v", *r.BladeId)) klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ResourceId", fmt.Sprintf("%+v", *r.ResourceId)) - serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.ip, r.ServiceTcp.port) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) if r.AllAppliances() && r.AllBlades() && r.AllResources() { summary, err = serviceWrap.GetResourceBlocks_AllApplsAllBlades(serviceClient) diff --git a/cli/pkg/serviceLib/serviceRequests/services.go b/cli/pkg/serviceLib/serviceRequests/services.go index f6c16058..1a858dc5 100644 --- a/cli/pkg/serviceLib/serviceRequests/services.go +++ b/cli/pkg/serviceLib/serviceRequests/services.go @@ -24,10 +24,10 @@ func NewServiceRequestListServiceInfo(cmd *cobra.Command) *ServiceRequestListSer } } -func (s *ServiceRequestListServiceInfo) Execute() (*service.ServiceInformation, error) { - klog.V(4).InfoS(fmt.Sprintf("%T", *s), "ServiceTcp", fmt.Sprintf("%+v", *s.ServiceTcp)) +func (r *ServiceRequestListServiceInfo) Execute() (*service.ServiceInformation, error) { + klog.V(4).InfoS(fmt.Sprintf("%T", *r), "ServiceTcp", fmt.Sprintf("%+v", *r.ServiceTcp)) - serviceClient := serviceWrap.GetServiceClient(s.ServiceTcp.GetIp(), s.ServiceTcp.GetPort()) + serviceClient := serviceWrap.GetServiceClient(r.ServiceTcp.GetIp(), r.ServiceTcp.GetPort(), r.ServiceTcp.GetInsecure(), r.ServiceTcp.GetProtocol()) serviceInfo, response, err := serviceClient.DefaultAPI.CfmV1Get(context.Background()).Execute() if err != nil { diff --git a/cli/pkg/serviceLib/serviceWrap/common.go b/cli/pkg/serviceLib/serviceWrap/common.go index 206ec1c6..deba5b14 100644 --- a/cli/pkg/serviceLib/serviceWrap/common.go +++ b/cli/pkg/serviceLib/serviceWrap/common.go @@ -3,29 +3,66 @@ package serviceWrap import ( + "crypto/tls" + "crypto/x509" "encoding/json" "errors" "fmt" + "log" "net/http" "net/netip" + "os" "strings" + "time" + "cfm/cli/pkg/serviceLib/flags" service "cfm/pkg/client" "github.com/google/uuid" "k8s.io/klog/v2" ) -func GetServiceClient(ip string, networkPort uint16) *service.APIClient { +const ( + SEAGATE_CFM_SERVICE_CRT_FILEPATH = "/usr/local/share/ca-certificates/github_com_seagate_cfm-self-signed.crt" +) + +func GetServiceClient(ip string, networkPort uint16, insecure bool, protocol string) *service.APIClient { // Instantiate new configuration using openapi funciton. config := service.NewConfiguration() - // Create, then pass, string for IP Address and Network port like "127.0.0.1:8080" + // Setup config basics config.Host = fmt.Sprintf("%s:%d", ip, networkPort) + config.Scheme = protocol + //TODO: Add this back in?? Check to see where this goes and if the service code is using it // // Pass debug value. // config.Debug = debug + if protocol == flags.PROTOCOL_HTTPS { + caCertPool := x509.NewCertPool() + + if !insecure { + // Load the cfm-service self-signed certificate + caCert, err := os.ReadFile(SEAGATE_CFM_SERVICE_CRT_FILEPATH) + if err != nil { + log.Fatal(err) + } + + caCertPool.AppendCertsFromPEM(caCert) + } + + // Create a custom HTTP client with the certificate pool + config.HTTPClient = &http.Client{ + Transport: &http.Transport{ + TLSClientConfig: &tls.Config{ + RootCAs: caCertPool, + InsecureSkipVerify: insecure, + }, + }, + Timeout: 30 * time.Second, + } + } + // This creates an API client, passing it the above configuration, and gathers a pointer to it. serviceClient := service.NewAPIClient(config) @@ -139,7 +176,7 @@ func NewApplianceBladeKey(applId, bladeId string) *ApplianceBladeKey { } } -//handleServiceError - common error handler function used after all cfm-service client calls. +// handleServiceError - common error handler function used after all cfm-service client calls. func handleServiceError(response *http.Response, err error) error { var status service.StatusMessage diff --git a/cmd/cfm-cli/cmd/composeBlade.go b/cmd/cfm-cli/cmd/composeBlade.go index f240d887..bee0150a 100644 --- a/cmd/cfm-cli/cmd/composeBlade.go +++ b/cmd/cfm-cli/cmd/composeBlade.go @@ -25,7 +25,7 @@ var composeBladeCmd = &cobra.Command{ request := serviceRequests.NewServiceRequestComposeMemory(cmd) - if request.GetPortId() != "" { + if request.PortId.GetId() != "" { err := common.PromptYesNo(common.WARNING_CXL_HOST_POWER_DOWN) if err != nil { return diff --git a/cmd/cfm-service/main.go b/cmd/cfm-service/main.go index 2ef87d48..d483d6ad 100644 --- a/cmd/cfm-service/main.go +++ b/cmd/cfm-service/main.go @@ -4,10 +4,12 @@ package main import ( "context" + "crypto/tls" "fmt" "log" "net/http" "os" + "os/exec" "strings" "sync" @@ -19,6 +21,7 @@ import ( "cfm/pkg/common/datastore" "cfm/pkg/openapi" "cfm/pkg/redfishapi" + "cfm/pkg/security" "cfm/services" ) @@ -106,7 +109,59 @@ func main() { } } + server, err := GenerateCfmServer(ctx, &settings, &handler) + if err != nil { + logger.Error(err, ", failed to generate cfm server: %s", err) + os.Exit(1) + } + // Start the main service logger.V(0).Info("cfm-service web server", "port", settings.Port) - log.Fatal(http.ListenAndServe(":"+settings.Port, handler)) + log.Fatal(server.ListenAndServeTLS("", "")) +} + +// GenerateCfmServer - Generates the primary cfm server using a runtine-generated self-signed certificate. +// Updates environmenetal variable SEAGATE_CFM_SERVICE_CRT_PATH. +// Saves the certificate to the SEAGATE_CFM_SERVICE_CRT_PATH location so that it can be shared with a local client. +func GenerateCfmServer(ctx context.Context, settings *common.Settings, handler *http.Handler) (*http.Server, error) { + logger := klog.FromContext(ctx) + + // Set environment variable (visible to webui but not cli (runs in different shell)) + err := os.Setenv("SEAGATE_CFM_SERVICE_CRT_PATH", security.SEAGATE_CFM_SERVICE_CRT_FILEPATH) + if err != nil { + return nil, fmt.Errorf("failure: setting environment variable: %v", err) + } + + // Generate the keys + cert, certPEM, err := security.GenerateSelfSignedCert() + if err != nil { + return nil, fmt.Errorf("failure: tls (self-signed) certificate generation: %v", err) + } + + // Write the certificate to a file + err = os.WriteFile(security.SEAGATE_CFM_SERVICE_CRT_FILEPATH, []byte(certPEM), 0644) + if err != nil { + return nil, fmt.Errorf("failure: tls cert file save: %v", err) + } + + logger.V(2).Info(fmt.Sprintf("cfm tls (self-signed) cert file saved to: %s ", security.SEAGATE_CFM_SERVICE_CRT_FILEPATH)) + + // Update CA certificates + cmd := exec.Command("update-ca-certificates") // This assumes the above self-signed .crt file is written to the correct location + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + return nil, fmt.Errorf("failure: update CA certificates: %v", err) + } + + // Configure the server + server := &http.Server{ + Addr: ":" + settings.Port, + TLSConfig: &tls.Config{ + Certificates: []tls.Certificate{*cert}, + }, + Handler: *handler, + } + + return server, nil } diff --git a/docker/Dockerfile b/docker/Dockerfile index 065dd690..91827a3d 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -46,7 +46,7 @@ RUN git apply api/patch/*.redfish.patch # build the excutable RUN make build-go -FROM node:alpine as npm +FROM node:alpine AS npm ENV BASEPATH=/cfm # copy source code with generated files to go image @@ -60,6 +60,9 @@ RUN npm run build FROM alpine:latest ENV BASEPATH=/cfm +# Install packages +RUN apk add --no-cache ca-certificates openssl bash curl jq + # copy source code with generated files to go image COPY --from=go ${BASEPATH}/cfm-service ${BASEPATH}/cfm-service COPY --from=go ${BASEPATH}/cfm-cli ${BASEPATH}/cfm-cli @@ -67,7 +70,7 @@ COPY --from=npm ${BASEPATH}/webui/dist ${BASEPATH}/webui/dist RUN ln -s /local/cfmdatastore.json ${BASEPATH}/cfmdatastore.json -LABEL org.opencontainers.image.source https://github.com/seagate/cfm +LABEL org.opencontainers.image.source=https://github.com/seagate/cfm WORKDIR ${BASEPATH} # Start the service diff --git a/pkg/security/certificates.go b/pkg/security/certificates.go new file mode 100644 index 00000000..9b70d76c --- /dev/null +++ b/pkg/security/certificates.go @@ -0,0 +1,74 @@ +// Copyright (c) 2024 Seagate Technology LLC and/or its Affiliates +package security + +import ( + "crypto/ecdsa" + "crypto/elliptic" + "crypto/rand" + "crypto/tls" + "crypto/x509" + "crypto/x509/pkix" + "encoding/pem" + "math/big" + "net" + "time" +) + +const ( + SEAGATE_CFM_SERVICE_CRT_FILEPATH = "/usr/local/share/ca-certificates/github_com_seagate_cfm-self-signed.crt" +) + +// GenerateSelfSignedCert - Generates the self-signed SSL/TLS certificate and private key at runtime. +func GenerateSelfSignedCert() (*tls.Certificate, []byte, error) { + priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + return nil, nil, err + } + + notBefore := time.Now() + notAfter := notBefore.Add(365 * 24 * time.Hour) + + serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128)) + if err != nil { + return nil, nil, err + } + + template := x509.Certificate{ + SerialNumber: serialNumber, + Subject: pkix.Name{ + Country: []string{"US"}, + Organization: []string{"SEAGATE TECHNOLOGY LLC"}, + OrganizationalUnit: []string{"MAG"}, + Locality: []string{"Longmont"}, + Province: []string{"Colorado"}, + CommonName: "localhost", // Set CommonName to a valid hostname + }, + NotBefore: notBefore, + NotAfter: notAfter, + + KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, + BasicConstraintsValid: true, + IPAddresses: []net.IP{net.ParseIP("127.0.0.1")}, // Add IP SAN + } + + derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv) + if err != nil { + return nil, nil, err + } + + certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) + keyPEM, err := x509.MarshalECPrivateKey(priv) + if err != nil { + return nil, nil, err + } + + keyPEMBytes := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyPEM}) + + cert, err := tls.X509KeyPair(certPEM, keyPEMBytes) + if err != nil { + return nil, nil, err + } + + return &cert, certPEM, nil +}