github.com/actions-on-google/gactions@v3.2.0+incompatible/cmd/gactions/cli/encrypt/encrypt.go (about)

     1  // Copyright 2020 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package encrypt provides an implementation of "gactions encrypt" command.
    16  package encrypt
    17  
    18  import (
    19  	"context"
    20  	"errors"
    21  	"syscall"
    22  
    23  	"github.com/actions-on-google/gactions/api/sdk"
    24  	"github.com/actions-on-google/gactions/log"
    25  	"github.com/actions-on-google/gactions/project"
    26  	"github.com/golang/crypto/ssh/terminal"
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  func askForSecret() (string, error) {
    31  	log.Outf("Write your secret: ")
    32  	secret, err := terminal.ReadPassword(int(syscall.Stdin))
    33  	if err != nil {
    34  		return "", err
    35  	}
    36  	return string(secret), nil
    37  }
    38  
    39  // AddCommand adds encrypt sub-command to the passed in root command.
    40  func AddCommand(ctx context.Context, root *cobra.Command, proj project.Project) {
    41  	encrypt := &cobra.Command{
    42  		Use:   "encrypt",
    43  		Short: "Encrypt client secret.",
    44  		Long:  "This commands encrypts the client secret key used in Account linking.",
    45  		RunE: func(cmd *cobra.Command, args []string) error {
    46  			if proj.ProjectRoot() == "" {
    47  				log.Errorf(`Can't find a project root. This may be because (1) %q was not found in this or any of the parent folders, or (2) if %q was found, but the key "sdkPath" was missing, or (3) if %q and manifest.yaml were both not found.`, project.ConfigName, project.ConfigName, project.ConfigName)
    48  				return errors.New("can not determine project root")
    49  			}
    50  			s, err := askForSecret()
    51  			if err != nil {
    52  				return err
    53  			}
    54  			return sdk.EncryptSecretJSON(ctx, proj, s)
    55  		},
    56  		Args: cobra.NoArgs,
    57  	}
    58  	root.AddCommand(encrypt)
    59  }