mirror of
https://github.com/gravitational/teleport.git
synced 2026-10-11 22:49:54 +00:00
All existing template usage was converted to make use of github.com/DataDog/datadog-agent/pkg/template. The DataDog package is a fork of the stdlib template packages with method calling removed so that reflect.MethodByName does not prevent DCE. There were a handful of method calls in our templates that were trivially changed to use custom functions defined via FuncMaps or specifying the calculated value to the template instead of calling a function in the template. A future change will add linter rules to prevent new text/template or html/imports from landing. They have not been included here so as to not to break enterprise code while it is migrated.
182 lines
5.0 KiB
Go
182 lines
5.0 KiB
Go
//go:build ignore
|
|
// +build ignore
|
|
|
|
// Teleport
|
|
// Copyright (C) 2025 Gravitational, Inc.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
template "github.com/DataDog/datadog-agent/pkg/template/text"
|
|
"github.com/gravitational/trace"
|
|
)
|
|
|
|
var tmpl = template.Must(template.New("generate").
|
|
Funcs(map[string]any{
|
|
"quote": func(v string) string {
|
|
return strconv.Quote(v)
|
|
},
|
|
}).
|
|
Parse(`
|
|
// Teleport
|
|
// Copyright (C) 2025 Gravitational, Inc.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Affero General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// Code generated by internal/region/codegen.go. DO NOT EDIT.
|
|
|
|
package aws
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
// Region represents an AWS region.
|
|
type Region struct {
|
|
// Name is the region name.
|
|
Name string
|
|
// Description is the human readable region name.
|
|
Description string
|
|
}
|
|
|
|
// regions is the set of available AWS regions based on the AWS SDK definition.
|
|
var regions = sync.OnceValue(func() map[string]Region {
|
|
return map[string]Region{
|
|
{{- range $name, $desc := $.Regions }}
|
|
{{ quote $name }}: Region{
|
|
Name: {{ quote $name }},
|
|
Description: {{ quote $desc }},
|
|
},
|
|
{{- end }}
|
|
}
|
|
})
|
|
`))
|
|
|
|
// TemplateData is the data passed to the template.
|
|
type TemplateData struct {
|
|
Regions map[string]string
|
|
}
|
|
|
|
// AWSPartitions contains the struct representation of the AWS SDK partitions
|
|
// JSON file.
|
|
type AWSPartitions struct {
|
|
Partitions []struct {
|
|
Regions map[string]struct {
|
|
Description string `json:"description"`
|
|
} `json:"regions"`
|
|
} `json:"partitions"`
|
|
}
|
|
|
|
func main() {
|
|
partitionsContents, err := loadSDKPartitionsFile()
|
|
if err != nil {
|
|
log.Fatalf("failed to parse partitions: %s", trace.DebugReport(err))
|
|
}
|
|
|
|
data, err := convertToTemplateData(partitionsContents)
|
|
if err != nil {
|
|
log.Fatalf("failed to parse partitions file: %s", trace.DebugReport(err))
|
|
}
|
|
|
|
file, err := os.Create(outputFilename)
|
|
if err != nil {
|
|
log.Fatalf("failed to create output file: %v", err)
|
|
}
|
|
defer func() {
|
|
if err := file.Close(); err != nil {
|
|
log.Fatalf("failed to close output file: %v", err)
|
|
}
|
|
}()
|
|
|
|
if err := tmpl.Execute(file, data); err != nil {
|
|
log.Fatalf("failed to generate file: %v", err)
|
|
}
|
|
}
|
|
|
|
func convertToTemplateData(partitionsContents []byte) (*TemplateData, error) {
|
|
var awsPartitions AWSPartitions
|
|
if err := json.Unmarshal(partitionsContents, &awsPartitions); err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
|
|
if len(awsPartitions.Partitions) == 0 {
|
|
return nil, trace.BadParameter("generated an empty regions list. check if the partitions JSON file format has changed")
|
|
}
|
|
|
|
data := &TemplateData{
|
|
Regions: make(map[string]string),
|
|
}
|
|
|
|
for _, partition := range awsPartitions.Partitions {
|
|
for name, region := range partition.Regions {
|
|
// For now, we're not including the global regions.
|
|
if strings.HasSuffix(name, awsGlobalRegionSuffix) {
|
|
continue
|
|
}
|
|
|
|
data.Regions[name] = region.Description
|
|
}
|
|
}
|
|
|
|
return data, nil
|
|
}
|
|
|
|
// loadSDKPartitionsFile loads the partitions JSON file from AWS SDK.
|
|
func loadSDKPartitionsFile() ([]byte, error) {
|
|
cmd := exec.Command("go", "list", "-m", "-f", "{{.Dir}}", awsPackageName)
|
|
awsPackageDir, err := cmd.Output()
|
|
if err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
|
|
contents, err := os.ReadFile(filepath.Join(strings.TrimSpace(string(awsPackageDir)), awsPartitionsFilePath))
|
|
if err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
|
|
return contents, nil
|
|
}
|
|
|
|
const (
|
|
// outputFilename is the generated filename.
|
|
outputFilename = "regions.go"
|
|
// awsPackageName is the AWS package used to find the paritions references.
|
|
awsPackageName = "github.com/aws/aws-sdk-go-v2"
|
|
// awsPartitionsFilePath is the partitions JSON file path inside the package.
|
|
awsPartitionsFilePath = "internal/endpoints/awsrulesfn/partitions.json"
|
|
// globalRegionSuffix is a strings suffix that indicates a region is a
|
|
// global region.
|
|
awsGlobalRegionSuffix = "-global"
|
|
)
|