github.com/GoogleContainerTools/kaniko@v1.23.0/pkg/buildcontext/azureblob.go (about)

     1  /*
     2  Copyright 2018 Google LLC
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package buildcontext
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"os"
    23  	"path/filepath"
    24  	"strings"
    25  
    26  	"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
    27  	kConfig "github.com/GoogleContainerTools/kaniko/pkg/config"
    28  	"github.com/GoogleContainerTools/kaniko/pkg/constants"
    29  	"github.com/GoogleContainerTools/kaniko/pkg/util"
    30  )
    31  
    32  // AzureBlob struct for Azure Blob Storage processing
    33  type AzureBlob struct {
    34  	context string
    35  }
    36  
    37  // Download context file from given azure blob storage url and unpack it to BuildContextDir
    38  func (b *AzureBlob) UnpackTarFromBuildContext() (string, error) {
    39  
    40  	// Get Azure_STORAGE_ACCESS_KEY from environment variables
    41  	accountKey := os.Getenv("AZURE_STORAGE_ACCESS_KEY")
    42  	if len(accountKey) == 0 {
    43  		return "", errors.New("AZURE_STORAGE_ACCESS_KEY environment variable is not set")
    44  	}
    45  
    46  	// Get storage accountName for Azure Blob Storage
    47  	parts, err := azblob.ParseURL(b.context)
    48  	if err != nil {
    49  		return parts.Host, err
    50  	}
    51  	accountName := strings.Split(parts.Host, ".")[0]
    52  
    53  	// Generate credential with accountName and accountKey
    54  	credential, err := azblob.NewSharedKeyCredential(accountName, accountKey)
    55  	if err != nil {
    56  		return parts.Host, err
    57  	}
    58  
    59  	// Create directory and target file for downloading the context file
    60  	directory := kConfig.BuildContextDir
    61  	tarPath := filepath.Join(directory, constants.ContextTar)
    62  	file, err := util.CreateTargetTarfile(tarPath)
    63  	if err != nil {
    64  		return tarPath, err
    65  	}
    66  
    67  	// Downloading context file from Azure Blob Storage
    68  	client, err := azblob.NewClientWithSharedKeyCredential(b.context, credential, nil)
    69  	if err != nil {
    70  		return parts.Host, err
    71  	}
    72  	ctx := context.Background()
    73  
    74  	if _, err := client.DownloadFile(ctx, parts.ContainerName, parts.BlobName, file, nil); err != nil {
    75  		return parts.Host, err
    76  	}
    77  
    78  	if err := util.UnpackCompressedTar(tarPath, directory); err != nil {
    79  		return tarPath, err
    80  	}
    81  	// Remove the tar so it doesn't interfere with subsequent commands
    82  	return directory, os.Remove(tarPath)
    83  }