publish.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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 compose
  14. import (
  15. "bytes"
  16. "context"
  17. "crypto/sha256"
  18. "encoding/json"
  19. "errors"
  20. "fmt"
  21. "io"
  22. "os"
  23. "github.com/DefangLabs/secret-detector/pkg/scanner"
  24. "github.com/DefangLabs/secret-detector/pkg/secrets"
  25. "github.com/compose-spec/compose-go/v2/loader"
  26. "github.com/compose-spec/compose-go/v2/types"
  27. "github.com/distribution/reference"
  28. "github.com/docker/cli/cli/command"
  29. "github.com/docker/compose/v2/internal/oci"
  30. "github.com/docker/compose/v2/pkg/api"
  31. "github.com/docker/compose/v2/pkg/compose/transform"
  32. "github.com/docker/compose/v2/pkg/progress"
  33. "github.com/docker/compose/v2/pkg/prompt"
  34. "github.com/opencontainers/go-digest"
  35. "github.com/opencontainers/image-spec/specs-go"
  36. v1 "github.com/opencontainers/image-spec/specs-go/v1"
  37. )
  38. func (s *composeService) Publish(ctx context.Context, project *types.Project, repository string, options api.PublishOptions) error {
  39. return progress.RunWithTitle(ctx, func(ctx context.Context) error {
  40. return s.publish(ctx, project, repository, options)
  41. }, s.stdinfo(), "Publishing")
  42. }
  43. //nolint:gocyclo
  44. func (s *composeService) publish(ctx context.Context, project *types.Project, repository string, options api.PublishOptions) error {
  45. accept, err := s.preChecks(project, options)
  46. if err != nil {
  47. return err
  48. }
  49. if !accept {
  50. return nil
  51. }
  52. err = s.Push(ctx, project, api.PushOptions{IgnoreFailures: true, ImageMandatory: true})
  53. if err != nil {
  54. return err
  55. }
  56. named, err := reference.ParseDockerRef(repository)
  57. if err != nil {
  58. return err
  59. }
  60. config := s.dockerCli.ConfigFile()
  61. resolver := oci.NewResolver(config)
  62. var layers []v1.Descriptor
  63. extFiles := map[string]string{}
  64. for _, file := range project.ComposeFiles {
  65. data, err := processFile(ctx, file, project, extFiles)
  66. if err != nil {
  67. return err
  68. }
  69. layerDescriptor := oci.DescriptorForComposeFile(file, data)
  70. layers = append(layers, layerDescriptor)
  71. }
  72. extLayers, err := processExtends(ctx, project, extFiles)
  73. if err != nil {
  74. return err
  75. }
  76. layers = append(layers, extLayers...)
  77. if options.WithEnvironment {
  78. layers = append(layers, envFileLayers(project)...)
  79. }
  80. if options.ResolveImageDigests {
  81. yaml, err := s.generateImageDigestsOverride(ctx, project)
  82. if err != nil {
  83. return err
  84. }
  85. layerDescriptor := oci.DescriptorForComposeFile("image-digests.yaml", yaml)
  86. layers = append(layers, layerDescriptor)
  87. }
  88. w := progress.ContextWriter(ctx)
  89. w.Event(progress.Event{
  90. ID: repository,
  91. Text: "publishing",
  92. Status: progress.Working,
  93. })
  94. if !s.dryRun {
  95. descriptor, err := oci.PushManifest(ctx, resolver, named, layers, options.OCIVersion)
  96. if err != nil {
  97. w.Event(progress.Event{
  98. ID: repository,
  99. Text: "publishing",
  100. Status: progress.Error,
  101. })
  102. return err
  103. }
  104. if options.Application {
  105. manifests := []v1.Descriptor{}
  106. for _, service := range project.Services {
  107. ref, err := reference.ParseDockerRef(service.Image)
  108. if err != nil {
  109. return err
  110. }
  111. manifest, err := oci.Copy(ctx, resolver, ref, named)
  112. if err != nil {
  113. return err
  114. }
  115. manifests = append(manifests, manifest)
  116. }
  117. descriptor.Data = nil
  118. index, err := json.Marshal(v1.Index{
  119. Versioned: specs.Versioned{SchemaVersion: 2},
  120. MediaType: v1.MediaTypeImageIndex,
  121. Manifests: manifests,
  122. Subject: &descriptor,
  123. Annotations: map[string]string{
  124. "com.docker.compose.version": api.ComposeVersion,
  125. },
  126. })
  127. if err != nil {
  128. return err
  129. }
  130. imagesDescriptor := v1.Descriptor{
  131. MediaType: v1.MediaTypeImageIndex,
  132. ArtifactType: oci.ComposeProjectArtifactType,
  133. Digest: digest.FromString(string(index)),
  134. Size: int64(len(index)),
  135. Annotations: map[string]string{
  136. "com.docker.compose.version": api.ComposeVersion,
  137. },
  138. Data: index,
  139. }
  140. err = oci.Push(ctx, resolver, reference.TrimNamed(named), imagesDescriptor)
  141. if err != nil {
  142. return err
  143. }
  144. }
  145. }
  146. w.Event(progress.Event{
  147. ID: repository,
  148. Text: "published",
  149. Status: progress.Done,
  150. })
  151. return nil
  152. }
  153. func processExtends(ctx context.Context, project *types.Project, extFiles map[string]string) ([]v1.Descriptor, error) {
  154. var layers []v1.Descriptor
  155. moreExtFiles := map[string]string{}
  156. for xf, hash := range extFiles {
  157. data, err := processFile(ctx, xf, project, moreExtFiles)
  158. if err != nil {
  159. return nil, err
  160. }
  161. layerDescriptor := oci.DescriptorForComposeFile(hash, data)
  162. layerDescriptor.Annotations["com.docker.compose.extends"] = "true"
  163. layers = append(layers, layerDescriptor)
  164. }
  165. for f, hash := range moreExtFiles {
  166. if _, ok := extFiles[f]; ok {
  167. delete(moreExtFiles, f)
  168. }
  169. extFiles[f] = hash
  170. }
  171. if len(moreExtFiles) > 0 {
  172. extLayers, err := processExtends(ctx, project, moreExtFiles)
  173. if err != nil {
  174. return nil, err
  175. }
  176. layers = append(layers, extLayers...)
  177. }
  178. return layers, nil
  179. }
  180. func processFile(ctx context.Context, file string, project *types.Project, extFiles map[string]string) ([]byte, error) {
  181. f, err := os.ReadFile(file)
  182. if err != nil {
  183. return nil, err
  184. }
  185. base, err := loader.LoadWithContext(ctx, types.ConfigDetails{
  186. WorkingDir: project.WorkingDir,
  187. Environment: project.Environment,
  188. ConfigFiles: []types.ConfigFile{
  189. {
  190. Filename: file,
  191. Content: f,
  192. },
  193. },
  194. }, func(options *loader.Options) {
  195. options.SkipValidation = true
  196. options.SkipExtends = true
  197. options.SkipConsistencyCheck = true
  198. options.ResolvePaths = true
  199. })
  200. if err != nil {
  201. return nil, err
  202. }
  203. for name, service := range base.Services {
  204. if service.Extends == nil {
  205. continue
  206. }
  207. xf := service.Extends.File
  208. if xf == "" {
  209. continue
  210. }
  211. if _, err = os.Stat(service.Extends.File); os.IsNotExist(err) {
  212. // No local file, while we loaded the project successfully: This is actually a remote resource
  213. continue
  214. }
  215. hash := fmt.Sprintf("%x.yaml", sha256.Sum256([]byte(xf)))
  216. extFiles[xf] = hash
  217. f, err = transform.ReplaceExtendsFile(f, name, hash)
  218. if err != nil {
  219. return nil, err
  220. }
  221. }
  222. return f, nil
  223. }
  224. func (s *composeService) generateImageDigestsOverride(ctx context.Context, project *types.Project) ([]byte, error) {
  225. project, err := project.WithProfiles([]string{"*"})
  226. if err != nil {
  227. return nil, err
  228. }
  229. project, err = project.WithImagesResolved(ImageDigestResolver(ctx, s.configFile(), s.apiClient()))
  230. if err != nil {
  231. return nil, err
  232. }
  233. override := types.Project{
  234. Services: types.Services{},
  235. }
  236. for name, service := range project.Services {
  237. override.Services[name] = types.ServiceConfig{
  238. Image: service.Image,
  239. }
  240. }
  241. return override.MarshalYAML()
  242. }
  243. //nolint:gocyclo
  244. func (s *composeService) preChecks(project *types.Project, options api.PublishOptions) (bool, error) {
  245. if ok, err := s.checkOnlyBuildSection(project); !ok || err != nil {
  246. return false, err
  247. }
  248. if options.AssumeYes {
  249. return true, nil
  250. }
  251. bindMounts := s.checkForBindMount(project)
  252. if len(bindMounts) > 0 {
  253. fmt.Println("you are about to publish bind mounts declaration within your OCI artifact.\n" +
  254. "only the bind mount declarations will be added to the OCI artifact (not content)\n" +
  255. "please double check that you are not mounting potential user's sensitive directories or data")
  256. for key, val := range bindMounts {
  257. _, _ = fmt.Fprintln(s.dockerCli.Out(), key)
  258. for _, v := range val {
  259. _, _ = fmt.Fprintf(s.dockerCli.Out(), "%s\n", v.String())
  260. }
  261. }
  262. if ok, err := acceptPublishBindMountDeclarations(s.dockerCli); err != nil || !ok {
  263. return false, err
  264. }
  265. }
  266. detectedSecrets, err := s.checkForSensitiveData(project)
  267. if err != nil {
  268. return false, err
  269. }
  270. if len(detectedSecrets) > 0 {
  271. fmt.Println("you are about to publish sensitive data within your OCI artifact.\n" +
  272. "please double check that you are not leaking sensitive data")
  273. for _, val := range detectedSecrets {
  274. _, _ = fmt.Fprintln(s.dockerCli.Out(), val.Type)
  275. _, _ = fmt.Fprintf(s.dockerCli.Out(), "%q: %s\n", val.Key, val.Value)
  276. }
  277. if ok, err := acceptPublishSensitiveData(s.dockerCli); err != nil || !ok {
  278. return false, err
  279. }
  280. }
  281. envVariables, err := s.checkEnvironmentVariables(project, options)
  282. if err != nil {
  283. return false, err
  284. }
  285. if len(envVariables) > 0 {
  286. fmt.Println("you are about to publish environment variables within your OCI artifact.\n" +
  287. "please double check that you are not leaking sensitive data")
  288. for key, val := range envVariables {
  289. _, _ = fmt.Fprintln(s.dockerCli.Out(), "Service/Config ", key)
  290. for k, v := range val {
  291. _, _ = fmt.Fprintf(s.dockerCli.Out(), "%s=%v\n", k, *v)
  292. }
  293. }
  294. if ok, err := acceptPublishEnvVariables(s.dockerCli); err != nil || !ok {
  295. return false, err
  296. }
  297. }
  298. return true, nil
  299. }
  300. func (s *composeService) checkEnvironmentVariables(project *types.Project, options api.PublishOptions) (map[string]types.MappingWithEquals, error) {
  301. envVarList := map[string]types.MappingWithEquals{}
  302. errorList := map[string][]string{}
  303. for _, service := range project.Services {
  304. if len(service.EnvFiles) > 0 {
  305. errorList[service.Name] = append(errorList[service.Name], fmt.Sprintf("service %q has env_file declared.", service.Name))
  306. }
  307. if len(service.Environment) > 0 {
  308. errorList[service.Name] = append(errorList[service.Name], fmt.Sprintf("service %q has environment variable(s) declared.", service.Name))
  309. envVarList[service.Name] = service.Environment
  310. }
  311. }
  312. for _, config := range project.Configs {
  313. if config.Environment != "" {
  314. errorList[config.Name] = append(errorList[config.Name], fmt.Sprintf("config %q is declare as an environment variable.", config.Name))
  315. envVarList[config.Name] = types.NewMappingWithEquals([]string{fmt.Sprintf("%s=%s", config.Name, config.Environment)})
  316. }
  317. }
  318. if !options.WithEnvironment && len(errorList) > 0 {
  319. errorMsgSuffix := "To avoid leaking sensitive data, you must either explicitly allow the sending of environment variables by using the --with-env flag,\n" +
  320. "or remove sensitive data from your Compose configuration"
  321. errorMsg := ""
  322. for _, errors := range errorList {
  323. for _, err := range errors {
  324. errorMsg += fmt.Sprintf("%s\n", err)
  325. }
  326. }
  327. return nil, fmt.Errorf("%s%s", errorMsg, errorMsgSuffix)
  328. }
  329. return envVarList, nil
  330. }
  331. func acceptPublishEnvVariables(cli command.Cli) (bool, error) {
  332. msg := "Are you ok to publish these environment variables? [y/N]: "
  333. confirm, err := prompt.NewPrompt(cli.In(), cli.Out()).Confirm(msg, false)
  334. return confirm, err
  335. }
  336. func acceptPublishSensitiveData(cli command.Cli) (bool, error) {
  337. msg := "Are you ok to publish these sensitive data? [y/N]: "
  338. confirm, err := prompt.NewPrompt(cli.In(), cli.Out()).Confirm(msg, false)
  339. return confirm, err
  340. }
  341. func acceptPublishBindMountDeclarations(cli command.Cli) (bool, error) {
  342. msg := "Are you ok to publish these bind mount declarations? [y/N]: "
  343. confirm, err := prompt.NewPrompt(cli.In(), cli.Out()).Confirm(msg, false)
  344. return confirm, err
  345. }
  346. func envFileLayers(project *types.Project) []v1.Descriptor {
  347. var layers []v1.Descriptor
  348. for _, service := range project.Services {
  349. for _, envFile := range service.EnvFiles {
  350. f, err := os.ReadFile(envFile.Path)
  351. if err != nil {
  352. // if we can't read the file, skip to the next one
  353. continue
  354. }
  355. layerDescriptor := oci.DescriptorForEnvFile(envFile.Path, f)
  356. layers = append(layers, layerDescriptor)
  357. }
  358. }
  359. return layers
  360. }
  361. func (s *composeService) checkOnlyBuildSection(project *types.Project) (bool, error) {
  362. errorList := []string{}
  363. for _, service := range project.Services {
  364. if service.Image == "" && service.Build != nil {
  365. errorList = append(errorList, service.Name)
  366. }
  367. }
  368. if len(errorList) > 0 {
  369. errMsg := "your Compose stack cannot be published as it only contains a build section for service(s):\n"
  370. for _, serviceInError := range errorList {
  371. errMsg += fmt.Sprintf("- %q\n", serviceInError)
  372. }
  373. return false, errors.New(errMsg)
  374. }
  375. return true, nil
  376. }
  377. func (s *composeService) checkForBindMount(project *types.Project) map[string][]types.ServiceVolumeConfig {
  378. allFindings := map[string][]types.ServiceVolumeConfig{}
  379. for serviceName, config := range project.Services {
  380. bindMounts := []types.ServiceVolumeConfig{}
  381. for _, volume := range config.Volumes {
  382. if volume.Type == types.VolumeTypeBind {
  383. bindMounts = append(bindMounts, volume)
  384. }
  385. }
  386. if len(bindMounts) > 0 {
  387. allFindings[serviceName] = bindMounts
  388. }
  389. }
  390. return allFindings
  391. }
  392. func (s *composeService) checkForSensitiveData(project *types.Project) ([]secrets.DetectedSecret, error) {
  393. var allFindings []secrets.DetectedSecret
  394. scan := scanner.NewDefaultScanner()
  395. // Check all compose files
  396. for _, file := range project.ComposeFiles {
  397. in, err := composeFileAsByteReader(file, project)
  398. if err != nil {
  399. return nil, err
  400. }
  401. findings, err := scan.ScanReader(in)
  402. if err != nil {
  403. return nil, fmt.Errorf("failed to scan compose file %s: %w", file, err)
  404. }
  405. allFindings = append(allFindings, findings...)
  406. }
  407. for _, service := range project.Services {
  408. // Check env files
  409. for _, envFile := range service.EnvFiles {
  410. findings, err := scan.ScanFile(envFile.Path)
  411. if err != nil {
  412. return nil, fmt.Errorf("failed to scan env file %s: %w", envFile.Path, err)
  413. }
  414. allFindings = append(allFindings, findings...)
  415. }
  416. }
  417. // Check configs defined by files
  418. for _, config := range project.Configs {
  419. if config.File != "" {
  420. findings, err := scan.ScanFile(config.File)
  421. if err != nil {
  422. return nil, fmt.Errorf("failed to scan config file %s: %w", config.File, err)
  423. }
  424. allFindings = append(allFindings, findings...)
  425. }
  426. }
  427. // Check secrets defined by files
  428. for _, secret := range project.Secrets {
  429. if secret.File != "" {
  430. findings, err := scan.ScanFile(secret.File)
  431. if err != nil {
  432. return nil, fmt.Errorf("failed to scan secret file %s: %w", secret.File, err)
  433. }
  434. allFindings = append(allFindings, findings...)
  435. }
  436. }
  437. return allFindings, nil
  438. }
  439. func composeFileAsByteReader(filePath string, project *types.Project) (io.Reader, error) {
  440. composeFile, err := os.ReadFile(filePath)
  441. if err != nil {
  442. return nil, fmt.Errorf("failed to open compose file %s: %w", filePath, err)
  443. }
  444. base, err := loader.LoadWithContext(context.TODO(), types.ConfigDetails{
  445. WorkingDir: project.WorkingDir,
  446. Environment: project.Environment,
  447. ConfigFiles: []types.ConfigFile{
  448. {
  449. Filename: filePath,
  450. Content: composeFile,
  451. },
  452. },
  453. }, func(options *loader.Options) {
  454. options.SkipValidation = true
  455. options.SkipExtends = true
  456. options.SkipConsistencyCheck = true
  457. options.ResolvePaths = true
  458. options.SkipInterpolation = true
  459. options.SkipResolveEnvironment = true
  460. })
  461. if err != nil {
  462. return nil, err
  463. }
  464. in, err := base.MarshalYAML()
  465. if err != nil {
  466. return nil, err
  467. }
  468. return bytes.NewBuffer(in), nil
  469. }