Skip to content

print https:// before public endpoints #1207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/cmd/cli/command/deploymentinfo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"regexp"
"strings"

"github.com/DefangLabs/defang/src/pkg/cli"
Expand Down Expand Up @@ -35,6 +36,8 @@ func printServiceStatesAndEndpoints(serviceInfos []*defangv1.ServiceInfo) error

showDomainNameColumn := false
showCertGenerateHint := false
hasPort := regexp.MustCompile(`:\d{1,5}$`)

for _, serviceInfo := range serviceInfos {
var domainname string
if serviceInfo.Domainname != "" {
Expand All @@ -44,12 +47,24 @@ func printServiceStatesAndEndpoints(serviceInfos []*defangv1.ServiceInfo) error
showCertGenerateHint = true
}
}
endpoints := make([]string, 0, len(serviceInfo.Endpoints))
for _, endpoint := range serviceInfo.Endpoints {
if !hasPort.MatchString(endpoint) {
endpoint = "https://" + endpoint
}

endpoints = append(endpoints, endpoint)
}
if len(endpoints) == 0 {
endpoints = append(endpoints, "N/A")
}

serviceTableItems = append(serviceTableItems, ServiceTableItem{
Deployment: serviceInfo.Etag,
Name: serviceInfo.Service.Name,
Status: serviceInfo.State.String(),
DomainName: domainname,
Endpoints: strings.Join(serviceInfo.Endpoints, ", "),
Endpoints: strings.Join(endpoints, ", "),
})
}

Expand Down
152 changes: 87 additions & 65 deletions src/cmd/cli/command/deploymentinfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestPrintPlaygroundPortalServiceURLs(t *testing.T) {
}
}

func TestPrintServiceStatesAndEndpoints(t *testing.T) {
func TestPrintServiceStatesAndEndpointsAndDomainname(t *testing.T) {
defaultTerm := term.DefaultTerm
t.Cleanup(func() {
term.DefaultTerm = defaultTerm
Expand All @@ -44,80 +44,102 @@ func TestPrintServiceStatesAndEndpoints(t *testing.T) {
var stdout, stderr bytes.Buffer
term.DefaultTerm = term.NewTerm(os.Stdin, &stdout, &stderr)

_ = printServiceStatesAndEndpoints([]*defangv1.ServiceInfo{
tests := []struct {
name string
serviceinfos []*defangv1.ServiceInfo
expectedLines []string
}{
{
Service: &defangv1.Service{
Name: "service1",
Ports: []*defangv1.Port{
{Mode: defangv1.Mode_INGRESS},
{Mode: defangv1.Mode_HOST},
name: "empty endpoint list",
serviceinfos: []*defangv1.ServiceInfo{
{
Service: &defangv1.Service{
Name: "service1",
Ports: []*defangv1.Port{
{Mode: defangv1.Mode_INGRESS},
{Mode: defangv1.Mode_HOST},
},
},
Status: "UNKNOWN",
Domainname: "example.com",
Endpoints: []string{},
},
},
Status: "UNKNOWN",
Endpoints: []string{
"example.com",
"service1.internal",
expectedLines: []string{
"Deployment Name Status Endpoints DomainName",
" service1 NOT_SPECIFIED N/A https://example.com",
" * Run `defang cert generate` to get a TLS certificate for your service(s)",
"",
},
}})
const expectedOutput = `Deployment Name Status Endpoints
service1 NOT_SPECIFIED example.com, service1.internal
`
receivedLines := strings.Split(stdout.String(), "\n")
expectedLines := strings.Split(expectedOutput, "\n")

if len(receivedLines) != len(expectedLines) {
t.Errorf("Expected %v lines, received %v", len(expectedLines), len(receivedLines))
}

for i, receivedLine := range receivedLines {
receivedLine = strings.TrimRight(receivedLine, " ")
if receivedLine != expectedLines[i] {
t.Errorf("\n-%v\n+%v", expectedLines[i], receivedLine)
}
}
}

func TestPrintServiceStatesAndEndpointsAndDomainname(t *testing.T) {
defaultTerm := term.DefaultTerm
t.Cleanup(func() {
term.DefaultTerm = defaultTerm
})

var stdout, stderr bytes.Buffer
term.DefaultTerm = term.NewTerm(os.Stdin, &stdout, &stderr)

_ = printServiceStatesAndEndpoints([]*defangv1.ServiceInfo{
},
{
Service: &defangv1.Service{
Name: "service1",
Ports: []*defangv1.Port{
{Mode: defangv1.Mode_INGRESS},
{Mode: defangv1.Mode_HOST},
name: "Service with Domainname",
serviceinfos: []*defangv1.ServiceInfo{
{
Service: &defangv1.Service{
Name: "service1",
Ports: []*defangv1.Port{
{Mode: defangv1.Mode_INGRESS},
{Mode: defangv1.Mode_HOST},
},
},
Status: "UNKNOWN",
Domainname: "example.com",
Endpoints: []string{
"example.com",
"service1.internal:80",
},
},
},
Status: "UNKNOWN",
Domainname: "example.com",
Endpoints: []string{
"example.com",
"service1.internal",
expectedLines: []string{
"Deployment Name Status Endpoints DomainName",
" service1 NOT_SPECIFIED https://example.com, service1.internal:80 https://example.com",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can't be be right, HTTPS to 80?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's not https to 80. it's https to example.com.

" * Run `defang cert generate` to get a TLS certificate for your service(s)",
"",
},
}})
expectedLines := []string{
"Deployment Name Status Endpoints DomainName",
" service1 NOT_SPECIFIED example.com, service1.internal https://example.com",
" * Run `defang cert generate` to get a TLS certificate for your service(s)",
"",
},
{
name: "endpoint without port",
serviceinfos: []*defangv1.ServiceInfo{
{
Service: &defangv1.Service{
Name: "service1",
Ports: []*defangv1.Port{
{Mode: defangv1.Mode_INGRESS},
{Mode: defangv1.Mode_HOST},
},
},
Status: "UNKNOWN",
Endpoints: []string{
"service1",
},
},
},
expectedLines: []string{
"Deployment Name Status Endpoints",
" service1 NOT_SPECIFIED https://service1",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lionello what's a good example of an endpoint that doesn't have a port, but which should be prefixed with https://? I don't think that this is a good example.

"",
},
},
}
receivedLines := strings.Split(stdout.String(), "\n")
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Reset stdout before each test
stdout.Reset()

if len(receivedLines) != len(expectedLines) {
t.Errorf("Expected %v lines, received %v", len(expectedLines), len(receivedLines))
}
_ = printServiceStatesAndEndpoints(tt.serviceinfos)
receivedLines := strings.Split(stdout.String(), "\n")

if len(receivedLines) != len(tt.expectedLines) {
t.Errorf("Expected %v lines, received %v", len(tt.expectedLines), len(receivedLines))
}

for i, receivedLine := range receivedLines {
receivedLine = strings.TrimRight(receivedLine, " ")
if receivedLine != expectedLines[i] {
t.Errorf("\n-%v\n+%v", expectedLines[i], receivedLine)
}
for i, receivedLine := range receivedLines {
receivedLine = strings.TrimRight(receivedLine, " ")
if receivedLine != tt.expectedLines[i] {
t.Errorf("\n-%v\n+%v", tt.expectedLines[i], receivedLine)
}
}
})
}
}
Loading