mirror of
https://github.com/drone-plugins/drone-docker.git
synced 2026-06-04 10:15:30 +08:00
fix: [CI-21707]: Remove EoL Components
This commit is contained in:
+41
-38
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
@@ -16,8 +17,6 @@ import (
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
|
||||
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
docker "github.com/drone-plugins/drone-docker"
|
||||
azureutil "github.com/drone-plugins/drone-docker/internal/azure"
|
||||
@@ -82,14 +81,15 @@ func main() {
|
||||
if username == "" && password == "" {
|
||||
// docker login credentials are not provided
|
||||
var err error
|
||||
username = defaultUsername
|
||||
if idToken != "" && clientId != "" && tenantId != "" {
|
||||
logrus.Debug("Using OIDC authentication flow")
|
||||
var aadToken string
|
||||
aadToken, err = azureutil.GetAADAccessTokenViaClientAssertion(context.Background(), tenantId, clientId, idToken, authorityHost)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
username = defaultUsername
|
||||
if idToken != "" && clientId != "" && tenantId != "" {
|
||||
slog.Debug("using OIDC authentication flow")
|
||||
var aadToken string
|
||||
aadToken, err = azureutil.GetAADAccessTokenViaClientAssertion(context.Background(), tenantId, clientId, idToken, authorityHost)
|
||||
if err != nil {
|
||||
slog.Error("failed to get AAD access token", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
var p string
|
||||
p, err = getPublicUrl(aadToken, registry, subscriptionId)
|
||||
if err == nil {
|
||||
@@ -97,16 +97,18 @@ func main() {
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "failed to get public url with error: %s\n", err)
|
||||
}
|
||||
password, err = fetchACRToken(tenantId, aadToken, registry)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
password, publicUrl, err = getAuth(clientId, clientSecret, clientCert, tenantId, subscriptionId, registry)
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
password, err = fetchACRToken(tenantId, aadToken, registry)
|
||||
if err != nil {
|
||||
slog.Error("failed to fetch ACR token", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
password, publicUrl, err = getAuth(clientId, clientSecret, clientCert, tenantId, subscriptionId, registry)
|
||||
if err != nil {
|
||||
slog.Error("failed to get auth", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// must use the fully qualified repo name. If the
|
||||
@@ -133,7 +135,8 @@ func main() {
|
||||
cmd.Stderr = os.Stderr
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
slog.Error("command execution failed", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,26 +156,26 @@ func getAuth(clientId, clientSecret, clientCert, tenantId, subscriptionId, regis
|
||||
if clientCert != "" {
|
||||
err := setupACRCert(clientCert, acrCertPath)
|
||||
if err != nil {
|
||||
errors.Wrap(err, "failed to push setup cert file")
|
||||
fmt.Errorf("failed to push setup cert file: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Get AZ env
|
||||
if err := os.Setenv(clientIdEnv, clientId); err != nil {
|
||||
return "", "", errors.Wrap(err, "failed to set env variable client Id")
|
||||
return "", "", fmt.Errorf("failed to set env variable client Id: %w", err)
|
||||
}
|
||||
if err := os.Setenv(clientSecretKeyEnv, clientSecret); err != nil {
|
||||
return "", "", errors.Wrap(err, "failed to set env variable client secret")
|
||||
return "", "", fmt.Errorf("failed to set env variable client secret: %w", err)
|
||||
}
|
||||
if err := os.Setenv(tenantKeyEnv, tenantId); err != nil {
|
||||
return "", "", errors.Wrap(err, "failed to set env variable tenant Id")
|
||||
return "", "", fmt.Errorf("failed to set env variable tenant Id: %w", err)
|
||||
}
|
||||
if err := os.Setenv(certPathEnv, acrCertPath); err != nil {
|
||||
return "", "", errors.Wrap(err, "failed to set env variable cert path")
|
||||
return "", "", fmt.Errorf("failed to set env variable cert path: %w", err)
|
||||
}
|
||||
env, err := azidentity.NewEnvironmentCredential(nil)
|
||||
if err != nil {
|
||||
return "", "", errors.Wrap(err, "failed to get env credentials from azure")
|
||||
return "", "", fmt.Errorf("failed to get env credentials from azure: %w", err)
|
||||
}
|
||||
os.Unsetenv(clientIdEnv)
|
||||
os.Unsetenv(clientSecretKeyEnv)
|
||||
@@ -185,7 +188,7 @@ func getAuth(clientId, clientSecret, clientCert, tenantId, subscriptionId, regis
|
||||
}
|
||||
aadToken, err := env.GetToken(context.Background(), policy)
|
||||
if err != nil {
|
||||
return "", "", errors.Wrap(err, "failed to fetch access token")
|
||||
return "", "", fmt.Errorf("failed to fetch access token: %w", err)
|
||||
}
|
||||
|
||||
// Get public URL for artifacts
|
||||
@@ -198,7 +201,7 @@ func getAuth(clientId, clientSecret, clientCert, tenantId, subscriptionId, regis
|
||||
// Fetch token
|
||||
ACRToken, err := fetchACRToken(tenantId, aadToken.Token, registry)
|
||||
if err != nil {
|
||||
return "", "", errors.Wrap(err, "failed to fetch ACR token")
|
||||
return "", "", fmt.Errorf("failed to fetch ACR token: %w", err)
|
||||
}
|
||||
return ACRToken, publicUrl, nil
|
||||
}
|
||||
@@ -213,14 +216,14 @@ func fetchACRToken(tenantId, token, registry string) (string, error) {
|
||||
}
|
||||
jsonResponse, err := http.PostForm(fmt.Sprintf("https://%s/oauth2/exchange", registry), formData)
|
||||
if err != nil || jsonResponse == nil {
|
||||
return "", errors.Wrap(err, "failed to fetch ACR token")
|
||||
return "", fmt.Errorf("failed to fetch ACR token: %w", err)
|
||||
}
|
||||
|
||||
// fetch token from response
|
||||
var response map[string]interface{}
|
||||
err = json.NewDecoder(jsonResponse.Body).Decode(&response)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to decode oauth exchange response")
|
||||
return "", fmt.Errorf("failed to decode oauth exchange response: %w", err)
|
||||
}
|
||||
|
||||
// Parse the refresh_token from the response
|
||||
@@ -228,19 +231,19 @@ func fetchACRToken(tenantId, token, registry string) (string, error) {
|
||||
if refreshToken, ok := t.(string); ok {
|
||||
return refreshToken, nil
|
||||
}
|
||||
return "", errors.New("failed to cast refresh token from acr")
|
||||
return "", fmt.Errorf("failed to cast refresh token from acr")
|
||||
}
|
||||
return "", errors.Wrap(err, "refresh token not found in response of oauth exchange call")
|
||||
return "", fmt.Errorf("refresh token not found in response of oauth exchange call: %w", err)
|
||||
}
|
||||
|
||||
func setupACRCert(cert, certPath string) error {
|
||||
decoded, err := base64.StdEncoding.DecodeString(cert)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to base64 decode ACR certificate")
|
||||
return fmt.Errorf("failed to base64 decode ACR certificate: %w", err)
|
||||
}
|
||||
err = ioutil.WriteFile(certPath, decoded, 0644)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to write ACR certificate")
|
||||
return fmt.Errorf("failed to write ACR certificate: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -262,24 +265,24 @@ func getPublicUrl(token, registryUrl, subscriptionId string) (string, error) {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return "", errors.Wrap(err, "failed to create request for getting container registry setting")
|
||||
return "", fmt.Errorf("failed to create request for getting container registry setting: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", "Bearer "+token)
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return "", errors.Wrap(err, "failed to send request for getting container registry setting")
|
||||
return "", fmt.Errorf("failed to send request for getting container registry setting: %w", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
var response subscriptionUrlResponse
|
||||
err = json.NewDecoder(res.Body).Decode(&response)
|
||||
if err != nil {
|
||||
return "", errors.Wrap(err, "failed to send request for getting container registry setting")
|
||||
return "", fmt.Errorf("failed to send request for getting container registry setting: %w", err)
|
||||
}
|
||||
if len(response.Value) == 0 {
|
||||
return "", errors.New("no id present for base url")
|
||||
return "", fmt.Errorf("no id present for base url")
|
||||
}
|
||||
return basePublicUrl + encodeParam(response.Value[0].ID), nil
|
||||
}
|
||||
|
||||
+12
-11
@@ -1,13 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
|
||||
"github.com/dchest/uniuri"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/urfave/cli"
|
||||
|
||||
docker "github.com/drone-plugins/drone-docker"
|
||||
@@ -358,7 +358,8 @@ func main() {
|
||||
}
|
||||
|
||||
if err := app.Run(os.Args); err != nil {
|
||||
logrus.Fatal(err)
|
||||
slog.Error("application error", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -448,16 +449,16 @@ func run(c *cli.Context) error {
|
||||
tag, err := docker.DefaultTagSuffix(
|
||||
c.String("commit.ref"),
|
||||
c.String("tags.suffix"),
|
||||
)
|
||||
if err != nil {
|
||||
logrus.Printf("cannot build docker image for %s, invalid semantic version", c.String("commit.ref"))
|
||||
return err
|
||||
}
|
||||
plugin.Build.Tags = tag
|
||||
} else {
|
||||
logrus.Printf("skipping automated docker build for %s", c.String("commit.ref"))
|
||||
return nil
|
||||
)
|
||||
if err != nil {
|
||||
slog.Error("cannot build docker image, invalid semantic version", "commit_ref", c.String("commit.ref"), "error", err)
|
||||
return err
|
||||
}
|
||||
plugin.Build.Tags = tag
|
||||
} else {
|
||||
slog.Info("skipping automated docker build", "commit_ref", c.String("commit.ref"))
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return plugin.Exec()
|
||||
|
||||
+12
-10
@@ -5,13 +5,13 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||
@@ -136,14 +136,15 @@ func main() {
|
||||
|
||||
repositoryName := trimHostname(repo, registry)
|
||||
for _, t := range tags {
|
||||
exists, err := tagExists(svc, repositoryName, t)
|
||||
if err != nil {
|
||||
logrus.Fatalf("Error checking if image exists for tag %s: %v", t, err)
|
||||
}
|
||||
if exists {
|
||||
logrus.Infof("%s:%s: Image tag exists. Skipping push.", repo, t)
|
||||
os.Exit(0)
|
||||
}
|
||||
exists, err := tagExists(svc, repositoryName, t)
|
||||
if err != nil {
|
||||
slog.Error("error checking if image exists for tag", "tag", t, "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if exists {
|
||||
slog.Info("image tag exists, skipping push", "repo", repo, "tag", t)
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,7 +153,8 @@ func main() {
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
if err = cmd.Run(); err != nil {
|
||||
logrus.Fatal(err)
|
||||
slog.Error("command execution failed", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
@@ -15,7 +16,6 @@ import (
|
||||
"github.com/drone-plugins/drone-docker/internal/gcp"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/sirupsen/logrus"
|
||||
"golang.org/x/oauth2"
|
||||
"golang.org/x/oauth2/google"
|
||||
)
|
||||
@@ -58,11 +58,13 @@ func loadConfig() Config {
|
||||
if idToken != "" && projectId != "" && poolId != "" && providerId != "" && serviceAccountEmail != "" {
|
||||
federalToken, err := gcp.GetFederalToken(idToken, projectId, poolId, providerId)
|
||||
if err != nil {
|
||||
logrus.Fatalf("Error (getFederalToken): %s", err)
|
||||
slog.Error("getFederalToken error", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
accessToken, err := gcp.GetGoogleCloudAccessToken(federalToken, serviceAccountEmail)
|
||||
if err != nil {
|
||||
logrus.Fatalf("Error (getGoogleCloudAccessToken): %s", err)
|
||||
slog.Error("getGoogleCloudAccessToken error", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
config.AccessToken = accessToken
|
||||
} else {
|
||||
@@ -110,7 +112,8 @@ func main() {
|
||||
cmd.Stderr = os.Stderr
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
slog.Error("command execution failed", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
@@ -14,7 +15,6 @@ import (
|
||||
"github.com/drone-plugins/drone-docker/internal/gcp"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/sirupsen/logrus"
|
||||
"golang.org/x/oauth2/google"
|
||||
)
|
||||
|
||||
@@ -48,11 +48,13 @@ func loadConfig() Config {
|
||||
if idToken != "" && projectId != "" && poolId != "" && providerId != "" && serviceAccountEmail != "" {
|
||||
federalToken, err := gcp.GetFederalToken(idToken, projectId, poolId, providerId)
|
||||
if err != nil {
|
||||
logrus.Fatalf("Error (getFederalToken): %s", err)
|
||||
slog.Error("getFederalToken error", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
accessToken, err := gcp.GetGoogleCloudAccessToken(federalToken, serviceAccountEmail)
|
||||
if err != nil {
|
||||
logrus.Fatalf("Error (getGoogleCloudAccessToken): %s", err)
|
||||
slog.Error("getGoogleCloudAccessToken error", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
config.AccessToken = accessToken
|
||||
} else {
|
||||
@@ -103,7 +105,8 @@ func main() {
|
||||
cmd.Stderr = os.Stderr
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
logrus.Fatal(err)
|
||||
slog.Error("command execution failed", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user