secrets.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package convert
  14. import (
  15. "encoding/base64"
  16. "fmt"
  17. "io/ioutil"
  18. "path"
  19. "strings"
  20. "github.com/Azure/azure-sdk-for-go/services/containerinstance/mgmt/2019-12-01/containerinstance"
  21. "github.com/Azure/go-autorest/autorest/to"
  22. "github.com/pkg/errors"
  23. )
  24. const (
  25. defaultSecretsPath = "/run/secrets"
  26. serviceSecretAbsPathPrefix = "aci-service-secret-path-"
  27. )
  28. func getServiceSecretKey(serviceName, targetDir string) string {
  29. return fmt.Sprintf("%s-%s--%s",
  30. serviceSecretAbsPathPrefix, serviceName, strings.ReplaceAll(targetDir, "/", "-"))
  31. }
  32. func (p projectAciHelper) getAciSecretVolumes() ([]containerinstance.Volume, error) {
  33. var secretVolumes []containerinstance.Volume
  34. for _, svc := range p.Services {
  35. squashedTargetVolumes := make(map[string]containerinstance.Volume)
  36. for _, scr := range svc.Secrets {
  37. data, err := ioutil.ReadFile(p.Secrets[scr.Source].File)
  38. if err != nil {
  39. return secretVolumes, err
  40. }
  41. if len(data) == 0 {
  42. continue
  43. }
  44. dataStr := base64.StdEncoding.EncodeToString(data)
  45. if scr.Target == "" {
  46. scr.Target = scr.Source
  47. }
  48. if !path.IsAbs(scr.Target) && strings.ContainsAny(scr.Target, "\\/") {
  49. return []containerinstance.Volume{},
  50. errors.Errorf("in service %q, secret with source %q cannot have a relative path as target. "+
  51. "Only absolute paths are allowed. Found %q",
  52. svc.Name, scr.Source, scr.Target)
  53. }
  54. if !path.IsAbs(scr.Target) {
  55. scr.Target = path.Join(defaultSecretsPath, scr.Target)
  56. }
  57. targetDir := path.Dir(scr.Target)
  58. targetDirKey := getServiceSecretKey(svc.Name, targetDir)
  59. if _, ok := squashedTargetVolumes[targetDir]; !ok {
  60. squashedTargetVolumes[targetDir] = containerinstance.Volume{
  61. Name: to.StringPtr(targetDirKey),
  62. Secret: make(map[string]*string),
  63. }
  64. }
  65. squashedTargetVolumes[targetDir].Secret[path.Base(scr.Target)] = &dataStr
  66. }
  67. for _, v := range squashedTargetVolumes {
  68. secretVolumes = append(secretVolumes, v)
  69. }
  70. }
  71. return secretVolumes, nil
  72. }
  73. func (s serviceConfigAciHelper) getAciSecretsVolumeMounts() ([]containerinstance.VolumeMount, error) {
  74. vms := []containerinstance.VolumeMount{}
  75. presenceSet := make(map[string]bool)
  76. for _, scr := range s.Secrets {
  77. if scr.Target == "" {
  78. scr.Target = scr.Source
  79. }
  80. if !path.IsAbs(scr.Target) {
  81. scr.Target = path.Join(defaultSecretsPath, scr.Target)
  82. }
  83. presenceKey := path.Dir(scr.Target)
  84. if !presenceSet[presenceKey] {
  85. vms = append(vms, containerinstance.VolumeMount{
  86. Name: to.StringPtr(getServiceSecretKey(s.Name, path.Dir(scr.Target))),
  87. MountPath: to.StringPtr(path.Dir(scr.Target)),
  88. ReadOnly: to.BoolPtr(true),
  89. })
  90. presenceSet[presenceKey] = true
  91. }
  92. }
  93. err := validateMountPathCollisions(vms)
  94. if err != nil {
  95. return []containerinstance.VolumeMount{}, err
  96. }
  97. return vms, nil
  98. }
  99. func validateMountPathCollisions(vms []containerinstance.VolumeMount) error {
  100. for i, vm1 := range vms {
  101. for j, vm2 := range vms {
  102. if i == j {
  103. continue
  104. }
  105. var (
  106. biggerVMPath = strings.Split(*vm1.MountPath, "/")
  107. smallerVMPath = strings.Split(*vm2.MountPath, "/")
  108. )
  109. if len(smallerVMPath) > len(biggerVMPath) {
  110. tmp := biggerVMPath
  111. biggerVMPath = smallerVMPath
  112. smallerVMPath = tmp
  113. }
  114. isPrefixed := true
  115. for i := 0; i < len(smallerVMPath); i++ {
  116. if smallerVMPath[i] != biggerVMPath[i] {
  117. isPrefixed = false
  118. break
  119. }
  120. }
  121. if isPrefixed {
  122. return errors.Errorf("mount paths %q and %q collide. A volume mount cannot include another one.", *vm1.MountPath, *vm2.MountPath)
  123. }
  124. }
  125. }
  126. return nil
  127. }