Skip to content
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
16 changes: 16 additions & 0 deletions pkg/asset/installconfig/aws/awserrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package aws

import (
"errors"
"net/http"

awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
"github.com/aws/smithy-go"
)

Expand All @@ -26,3 +28,17 @@ func IsUnauthorized(err error) bool {
}
return false
}

// IsHTTPForbidden returns true if and only if the error is an HTTP
// 403 error from the AWS API.
func IsHTTPForbidden(err error) bool {
if err == nil {
return false
}

var respErr *awshttp.ResponseError
if errors.As(err, &respErr) {
return respErr.HTTPStatusCode() == http.StatusForbidden
}
return false
}
76 changes: 30 additions & 46 deletions pkg/asset/installconfig/aws/basedomain.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,40 @@
package aws

import (
"context"
"errors"
"fmt"
"net/http"
"sort"
"strings"

survey "github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/core"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/route53"
route53types "github.com/aws/aws-sdk-go-v2/service/route53/types"
"github.com/sirupsen/logrus"
)

// IsForbidden returns true if and only if the input error is an HTTP
// 403 error from the AWS API.
func IsForbidden(err error) bool {
var requestError awserr.RequestFailure
return errors.As(err, &requestError) && requestError.StatusCode() == http.StatusForbidden
}

// GetBaseDomain returns a base domain chosen from among the account's
// public routes.
func GetBaseDomain() (string, error) {
session, err := GetSession()
if err != nil {
return "", err
}

func GetBaseDomain(ctx context.Context, client *route53.Client) (string, error) {
logrus.Debugf("listing AWS hosted zones")
client := route53.New(session)

publicZoneMap := map[string]struct{}{}
exists := struct{}{}
if err := client.ListHostedZonesPages(
&route53.ListHostedZonesInput{},
func(resp *route53.ListHostedZonesOutput, lastPage bool) (shouldContinue bool) {
for _, zone := range resp.HostedZones {
if zone.Config != nil && !aws.BoolValue(zone.Config.PrivateZone) {
publicZoneMap[strings.TrimSuffix(*zone.Name, ".")] = exists
}

paginator := route53.NewListHostedZonesPaginator(client, &route53.ListHostedZonesInput{})
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
return "", fmt.Errorf("failed to list hosted zones: %w", err)
}

for _, zone := range page.HostedZones {
if zone.Config != nil && !zone.Config.PrivateZone {
publicZoneMap[strings.TrimSuffix(aws.ToString(zone.Name), ".")] = exists
}
return !lastPage
},
); err != nil {
return "", fmt.Errorf("list hosted zones: %w", err)
}
}

publicZones := make([]string, 0, len(publicZoneMap))
Expand Down Expand Up @@ -82,24 +70,20 @@ func GetBaseDomain() (string, error) {
}

// GetPublicZone returns a public route53 zone that matches the name.
func GetPublicZone(sess *session.Session, name string) (*route53.HostedZone, error) {
var res *route53.HostedZone
f := func(resp *route53.ListHostedZonesOutput, lastPage bool) (shouldContinue bool) {
for idx, zone := range resp.HostedZones {
if zone.Config != nil && !aws.BoolValue(zone.Config.PrivateZone) && strings.TrimSuffix(aws.StringValue(zone.Name), ".") == strings.TrimSuffix(name, ".") {
res = resp.HostedZones[idx]
return false
func GetPublicZone(ctx context.Context, client *route53.Client, name string) (*route53types.HostedZone, error) {
paginator := route53.NewListHostedZonesPaginator(client, &route53.ListHostedZonesInput{})
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list hosted zones: %w", err)
}

for _, zone := range page.HostedZones {
if zone.Config != nil && !zone.Config.PrivateZone && strings.TrimSuffix(aws.ToString(zone.Name), ".") == strings.TrimSuffix(name, ".") {
return &zone, nil
}
}
return !lastPage
}

client := route53.New(sess)
if err := client.ListHostedZonesPages(&route53.ListHostedZonesInput{}, f); err != nil {
return nil, fmt.Errorf("listing hosted zones: %w", err)
}
if res == nil {
return nil, fmt.Errorf("no public route53 zone found matching name %q", name)
}
return res, nil
return nil, fmt.Errorf("no public route53 zone found matching name %q", name)
}
9 changes: 0 additions & 9 deletions pkg/asset/installconfig/aws/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ import (
typesaws "github.com/openshift/installer/pkg/types/aws"
)

// Partition identifiers.
const (
AwsPartitionID = "aws" // AWS Standard partition.
AwsCnPartitionID = "aws-cn" // AWS China partition.
AwsUsGovPartitionID = "aws-us-gov" // AWS GovCloud (US) partition.
AwsIsoPartitionID = "aws-iso" // AWS ISO (US) partition.
AwsIsoBPartitionID = "aws-iso-b" // AWS ISOB (US) partition.
)

var (
// v1Tov2ServiceIDMap maps v1 service ID to its v2 equivalent.
v1Tov2ServiceIDMap = map[string]string{
Expand Down
63 changes: 32 additions & 31 deletions pkg/asset/installconfig/aws/mock/awsroute53_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 2 additions & 6 deletions pkg/asset/installconfig/aws/regions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import (

"github.com/openshift/installer/pkg/rhcos"
"github.com/openshift/installer/pkg/types"
)

const (
isoPartition = "aws-iso"
isobPartition = "aws-iso-b"
typesaws "github.com/openshift/installer/pkg/types/aws"
)

// knownPublicRegions is the subset of public AWS regions where RHEL CoreOS images are published.
Expand Down Expand Up @@ -54,7 +50,7 @@ func IsSecretRegion(region string) (bool, error) {
}

switch endpoint.PartitionID {
case isoPartition, isobPartition:
case typesaws.AwsIsoPartitionID, typesaws.AwsIsoBPartitionID:
return true, nil
}

Expand Down
Loading