github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/api/handlers/compat/secrets.go (about)

     1  package compat
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/base64"
     6  	"encoding/json"
     7  	"net/http"
     8  
     9  	"github.com/hanks177/podman/v4/libpod"
    10  	"github.com/hanks177/podman/v4/pkg/api/handlers/utils"
    11  	api "github.com/hanks177/podman/v4/pkg/api/types"
    12  	"github.com/hanks177/podman/v4/pkg/domain/entities"
    13  	"github.com/hanks177/podman/v4/pkg/domain/infra/abi"
    14  	"github.com/hanks177/podman/v4/pkg/util"
    15  	"github.com/pkg/errors"
    16  )
    17  
    18  func ListSecrets(w http.ResponseWriter, r *http.Request) {
    19  	runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
    20  	filtersMap, err := util.PrepareFilters(r)
    21  	if err != nil {
    22  		utils.Error(w, http.StatusInternalServerError, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
    23  		return
    24  	}
    25  	ic := abi.ContainerEngine{Libpod: runtime}
    26  	listOptions := entities.SecretListRequest{
    27  		Filters: *filtersMap,
    28  	}
    29  	reports, err := ic.SecretList(r.Context(), listOptions)
    30  	if err != nil {
    31  		utils.InternalServerError(w, err)
    32  		return
    33  	}
    34  	if utils.IsLibpodRequest(r) {
    35  		utils.WriteResponse(w, http.StatusOK, reports)
    36  		return
    37  	}
    38  	// Docker compat expects a version field that increments when the secret is updated
    39  	// We currently can't update a secret, so we default the version to 1
    40  	compatReports := make([]entities.SecretInfoReportCompat, 0, len(reports))
    41  	for _, report := range reports {
    42  		compatRep := entities.SecretInfoReportCompat{
    43  			SecretInfoReport: *report,
    44  			Version:          entities.SecretVersion{Index: 1},
    45  		}
    46  		compatReports = append(compatReports, compatRep)
    47  	}
    48  	utils.WriteResponse(w, http.StatusOK, compatReports)
    49  }
    50  
    51  func InspectSecret(w http.ResponseWriter, r *http.Request) {
    52  	runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
    53  	name := utils.GetName(r)
    54  	names := []string{name}
    55  	ic := abi.ContainerEngine{Libpod: runtime}
    56  	reports, errs, err := ic.SecretInspect(r.Context(), names)
    57  	if err != nil {
    58  		utils.InternalServerError(w, err)
    59  		return
    60  	}
    61  	if len(errs) > 0 {
    62  		utils.SecretNotFound(w, name, errs[0])
    63  		return
    64  	}
    65  	if len(reports) < 1 {
    66  		utils.InternalServerError(w, err)
    67  		return
    68  	}
    69  	if utils.IsLibpodRequest(r) {
    70  		utils.WriteResponse(w, http.StatusOK, reports[0])
    71  		return
    72  	}
    73  	// Docker compat expects a version field that increments when the secret is updated
    74  	// We currently can't update a secret, so we default the version to 1
    75  	compatReport := entities.SecretInfoReportCompat{
    76  		SecretInfoReport: *reports[0],
    77  		Version:          entities.SecretVersion{Index: 1},
    78  	}
    79  	utils.WriteResponse(w, http.StatusOK, compatReport)
    80  }
    81  
    82  func RemoveSecret(w http.ResponseWriter, r *http.Request) {
    83  	runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
    84  
    85  	opts := entities.SecretRmOptions{}
    86  	name := utils.GetName(r)
    87  	ic := abi.ContainerEngine{Libpod: runtime}
    88  	reports, err := ic.SecretRm(r.Context(), []string{name}, opts)
    89  	if err != nil {
    90  		utils.InternalServerError(w, err)
    91  		return
    92  	}
    93  	if reports[0].Err != nil {
    94  		utils.SecretNotFound(w, name, reports[0].Err)
    95  		return
    96  	}
    97  	utils.WriteResponse(w, http.StatusNoContent, nil)
    98  }
    99  
   100  func CreateSecret(w http.ResponseWriter, r *http.Request) {
   101  	runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
   102  	opts := entities.SecretCreateOptions{}
   103  	createParams := struct {
   104  		*entities.SecretCreateRequest
   105  		Labels map[string]string `schema:"labels"`
   106  	}{}
   107  
   108  	if err := json.NewDecoder(r.Body).Decode(&createParams); err != nil {
   109  		utils.Error(w, http.StatusInternalServerError, errors.Wrap(err, "Decode()"))
   110  		return
   111  	}
   112  	if len(createParams.Labels) > 0 {
   113  		utils.Error(w, http.StatusBadRequest, errors.Wrapf(errors.New("bad parameter"), "labels not supported"))
   114  		return
   115  	}
   116  
   117  	decoded, _ := base64.StdEncoding.DecodeString(createParams.Data)
   118  	reader := bytes.NewReader(decoded)
   119  	opts.Driver = createParams.Driver.Name
   120  
   121  	ic := abi.ContainerEngine{Libpod: runtime}
   122  	report, err := ic.SecretCreate(r.Context(), createParams.Name, reader, opts)
   123  	if err != nil {
   124  		if errors.Cause(err).Error() == "secret name in use" {
   125  			utils.Error(w, http.StatusConflict, err)
   126  			return
   127  		}
   128  		utils.InternalServerError(w, err)
   129  		return
   130  	}
   131  	utils.WriteResponse(w, http.StatusOK, report)
   132  }
   133  
   134  func UpdateSecret(w http.ResponseWriter, r *http.Request) {
   135  	utils.Error(w, http.StatusNotImplemented, errors.New("update is not supported"))
   136  }