Compare commits
1 Commits
develop
...
release/2.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
244f34f11f |
3
.eslintignore
Normal file
3
.eslintignore
Normal file
@@ -0,0 +1,3 @@
|
||||
node_modules/
|
||||
dist/
|
||||
test/
|
||||
151
.eslintrc.yml
Normal file
151
.eslintrc.yml
Normal file
@@ -0,0 +1,151 @@
|
||||
env:
|
||||
browser: true
|
||||
jquery: true
|
||||
node: true
|
||||
es6: true
|
||||
|
||||
globals:
|
||||
angular: true
|
||||
|
||||
extends:
|
||||
- 'eslint:recommended'
|
||||
- 'plugin:storybook/recommended'
|
||||
- 'plugin:import/typescript'
|
||||
- prettier
|
||||
|
||||
plugins:
|
||||
- import
|
||||
|
||||
parserOptions:
|
||||
ecmaVersion: 2018
|
||||
sourceType: module
|
||||
project: './tsconfig.json'
|
||||
ecmaFeatures:
|
||||
modules: true
|
||||
|
||||
rules:
|
||||
no-console: error
|
||||
no-alert: error
|
||||
no-control-regex: 'off'
|
||||
no-empty: warn
|
||||
no-empty-function: warn
|
||||
no-useless-escape: 'off'
|
||||
import/named: error
|
||||
import/order:
|
||||
[
|
||||
'error',
|
||||
{
|
||||
pathGroups:
|
||||
[
|
||||
{ pattern: '@@/**', group: 'internal', position: 'after' },
|
||||
{ pattern: '@/**', group: 'internal' },
|
||||
{ pattern: '{Kubernetes,Portainer,Agent,Azure,Docker}/**', group: 'internal' },
|
||||
],
|
||||
groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index'],
|
||||
pathGroupsExcludedImportTypes: ['internal'],
|
||||
},
|
||||
]
|
||||
no-restricted-imports:
|
||||
- error
|
||||
- patterns:
|
||||
- group:
|
||||
- '@/react/test-utils/*'
|
||||
message: 'These utils are just for test files'
|
||||
|
||||
settings:
|
||||
'import/resolver':
|
||||
alias:
|
||||
map:
|
||||
- ['@@', './app/react/components']
|
||||
- ['@', './app']
|
||||
extensions: ['.js', '.ts', '.tsx']
|
||||
typescript: true
|
||||
node: true
|
||||
|
||||
overrides:
|
||||
- files:
|
||||
- app/**/*.ts{,x}
|
||||
parserOptions:
|
||||
project: './tsconfig.json'
|
||||
parser: '@typescript-eslint/parser'
|
||||
plugins:
|
||||
- '@typescript-eslint'
|
||||
- 'regex'
|
||||
extends:
|
||||
- airbnb
|
||||
- airbnb-typescript
|
||||
- 'plugin:eslint-comments/recommended'
|
||||
- 'plugin:react-hooks/recommended'
|
||||
- 'plugin:react/jsx-runtime'
|
||||
- 'plugin:@typescript-eslint/recommended'
|
||||
- 'plugin:@typescript-eslint/eslint-recommended'
|
||||
- 'plugin:promise/recommended'
|
||||
- 'plugin:storybook/recommended'
|
||||
- prettier # should be last
|
||||
settings:
|
||||
react:
|
||||
version: 'detect'
|
||||
|
||||
rules:
|
||||
no-console: error
|
||||
import/order:
|
||||
[
|
||||
'error',
|
||||
{
|
||||
pathGroups: [{ pattern: '@@/**', group: 'internal', position: 'after' }, { pattern: '@/**', group: 'internal' }],
|
||||
groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index'],
|
||||
'newlines-between': 'always',
|
||||
},
|
||||
]
|
||||
no-plusplus: off
|
||||
func-style: [error, 'declaration']
|
||||
import/prefer-default-export: off
|
||||
no-use-before-define: 'off'
|
||||
'@typescript-eslint/no-use-before-define': ['error', { functions: false, 'allowNamedExports': true }]
|
||||
no-shadow: 'off'
|
||||
'@typescript-eslint/no-shadow': off
|
||||
jsx-a11y/no-autofocus: warn
|
||||
react/forbid-prop-types: off
|
||||
react/require-default-props: off
|
||||
react/no-array-index-key: off
|
||||
no-underscore-dangle: off
|
||||
react/jsx-filename-extension: [0]
|
||||
import/no-extraneous-dependencies: ['error', { devDependencies: true }]
|
||||
'@typescript-eslint/explicit-module-boundary-types': off
|
||||
'@typescript-eslint/no-unused-vars': 'error'
|
||||
'@typescript-eslint/no-explicit-any': 'error'
|
||||
'jsx-a11y/label-has-associated-control': ['error', { 'assert': 'either', controlComponents: ['Input', 'Checkbox'] }]
|
||||
'react/function-component-definition': ['error', { 'namedComponents': 'function-declaration' }]
|
||||
'react/jsx-no-bind': off
|
||||
'no-await-in-loop': 'off'
|
||||
'react/jsx-no-useless-fragment': ['error', { allowExpressions: true }]
|
||||
'regex/invalid': ['error', [{ 'regex': '<Icon icon="(.*)"', 'message': 'Please directly import the `lucide-react` icon instead of using the string' }]]
|
||||
'@typescript-eslint/no-restricted-imports':
|
||||
- error
|
||||
- patterns:
|
||||
- group:
|
||||
- '@/react/test-utils/*'
|
||||
message: 'These utils are just for test files'
|
||||
overrides: # allow props spreading for hoc files
|
||||
- files:
|
||||
- app/**/with*.ts{,x}
|
||||
rules:
|
||||
'react/jsx-props-no-spreading': off
|
||||
- files:
|
||||
- app/**/*.test.*
|
||||
extends:
|
||||
- 'plugin:vitest/recommended'
|
||||
env:
|
||||
'vitest/env': true
|
||||
rules:
|
||||
'react/jsx-no-constructed-context-values': off
|
||||
'@typescript-eslint/no-restricted-imports': off
|
||||
no-restricted-imports: off
|
||||
'react/jsx-props-no-spreading': off
|
||||
- files:
|
||||
- app/**/*.stories.*
|
||||
rules:
|
||||
'no-alert': off
|
||||
'@typescript-eslint/no-restricted-imports': off
|
||||
no-restricted-imports: off
|
||||
'react/jsx-props-no-spreading': off
|
||||
4
.github/DISCUSSION_TEMPLATE/ideas.yaml
vendored
4
.github/DISCUSSION_TEMPLATE/ideas.yaml
vendored
@@ -3,13 +3,13 @@ body:
|
||||
attributes:
|
||||
value: |
|
||||
# Welcome!
|
||||
|
||||
|
||||
Thanks for suggesting an idea for Portainer!
|
||||
|
||||
Before opening a new idea or feature request, make sure that we do not have any duplicates already open. You can ensure this by [searching this discussion category](https://github.com/orgs/portainer/discussions/categories/ideas). If there is a duplicate, please add a comment to the existing idea instead.
|
||||
|
||||
Also, be sure to check our [knowledge base](https://portal.portainer.io/knowledge) and [documentation](https://docs.portainer.io) as they may point you toward a solution.
|
||||
|
||||
|
||||
**DO NOT FILE DUPLICATE REQUESTS.**
|
||||
|
||||
- type: textarea
|
||||
|
||||
55
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
55
.github/ISSUE_TEMPLATE/bug_report.yml
vendored
@@ -22,7 +22,7 @@ body:
|
||||
options:
|
||||
- label: Yes, I've searched similar issues on [GitHub](https://github.com/portainer/portainer/issues).
|
||||
required: true
|
||||
- label: Yes, I've checked whether this issue is covered in the Portainer [documentation](https://docs.portainer.io).
|
||||
- label: Yes, I've checked whether this issue is covered in the Portainer [documentation](https://docs.portainer.io) or [knowledge base](https://portal.portainer.io/knowledge).
|
||||
required: true
|
||||
|
||||
- type: markdown
|
||||
@@ -94,30 +94,49 @@ body:
|
||||
description: We only provide support for current versions of Portainer as per the lifecycle policy linked above. If you are on an older version of Portainer we recommend [updating first](https://docs.portainer.io/start/upgrade) in case your bug has already been fixed.
|
||||
multiple: false
|
||||
options:
|
||||
- '2.42.0'
|
||||
- '2.41.1'
|
||||
- '2.41.0'
|
||||
- '2.40.0'
|
||||
- '2.39.3'
|
||||
- '2.39.2'
|
||||
- '2.39.1'
|
||||
- '2.39.0'
|
||||
- '2.38.1'
|
||||
- '2.38.0'
|
||||
- '2.37.0'
|
||||
- '2.36.0'
|
||||
- '2.35.0'
|
||||
- '2.34.0'
|
||||
- '2.33.8'
|
||||
- '2.33.7'
|
||||
- '2.33.6'
|
||||
- '2.33.5'
|
||||
- '2.33.4'
|
||||
- '2.33.3'
|
||||
- '2.33.2'
|
||||
- '2.33.1'
|
||||
- '2.33.0'
|
||||
|
||||
- '2.32.0'
|
||||
- '2.31.3'
|
||||
- '2.31.2'
|
||||
- '2.31.1'
|
||||
- '2.31.0'
|
||||
- '2.30.1'
|
||||
- '2.30.0'
|
||||
- '2.29.2'
|
||||
- '2.29.1'
|
||||
- '2.29.0'
|
||||
- '2.28.1'
|
||||
- '2.28.0'
|
||||
- '2.27.9'
|
||||
- '2.27.8'
|
||||
- '2.27.7'
|
||||
- '2.27.6'
|
||||
- '2.27.5'
|
||||
- '2.27.4'
|
||||
- '2.27.3'
|
||||
- '2.27.2'
|
||||
- '2.27.1'
|
||||
- '2.27.0'
|
||||
- '2.26.1'
|
||||
- '2.26.0'
|
||||
- '2.25.1'
|
||||
- '2.25.0'
|
||||
- '2.24.1'
|
||||
- '2.24.0'
|
||||
- '2.23.0'
|
||||
- '2.22.0'
|
||||
- '2.21.5'
|
||||
- '2.21.4'
|
||||
- '2.21.3'
|
||||
- '2.21.2'
|
||||
- '2.21.1'
|
||||
- '2.21.0'
|
||||
validations:
|
||||
required: true
|
||||
|
||||
|
||||
86
.github/workflows/build-image.yml
vendored
86
.github/workflows/build-image.yml
vendored
@@ -1,86 +0,0 @@
|
||||
name: Build image
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [develop]
|
||||
tags: ['v*']
|
||||
workflow_dispatch: {}
|
||||
|
||||
env:
|
||||
IMAGE: ghcr.io/vvzvlad/portainer-ce
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup pnpm
|
||||
uses: pnpm/action-setup@v4
|
||||
|
||||
- name: Setup Node
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 22
|
||||
|
||||
- name: Setup Go
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version-file: go.mod
|
||||
|
||||
- name: Resolve version
|
||||
id: ver
|
||||
run: echo "version=$(node -p "require('./package.json').version")" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Install client dependencies
|
||||
# CI forces pnpm into --frozen-lockfile, which fails with
|
||||
# ERR_PNPM_LOCKFILE_CONFIG_MISMATCH because the committed lockfile lacks
|
||||
# the pnpmfileChecksum for the configDependencies in package.json.
|
||||
# Reconcile the lockfile explicitly; the later frozen install in
|
||||
# `make client-deps` then finds a matching lockfile. pnpm ignores the
|
||||
# npm_config_frozen_lockfile env var, so an explicit flag is required.
|
||||
run: pnpm install --no-frozen-lockfile
|
||||
|
||||
- name: Build client and server
|
||||
env:
|
||||
SKIP_GO_GET: "true"
|
||||
CONTAINER_IMAGE_TAG: ${{ steps.ver.outputs.version }}
|
||||
BUILDNUMBER: ${{ github.run_number }}
|
||||
# Pin the embedded commit to the full SHA so it matches the image
|
||||
# GIT_COMMIT build-arg and does not depend on the shallow checkout.
|
||||
GIT_COMMIT_HASH: ${{ github.sha }}
|
||||
# ENV=production selects webpack/webpack.production.js (minified bundle),
|
||||
# matching the official CE image; the Makefile default is development.
|
||||
run: make build-all ENV=production
|
||||
|
||||
- name: Ensure storybook directory exists
|
||||
# make build-all does not produce dist/storybook, but alpine.Dockerfile
|
||||
# has `COPY dist/storybook* /storybook/`; without a match the docker build fails.
|
||||
run: mkdir -p dist/storybook
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@v3
|
||||
|
||||
- name: Log in to GHCR
|
||||
uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
- name: Build and push image (linux/amd64, alpine base)
|
||||
uses: docker/build-push-action@v6
|
||||
with:
|
||||
context: .
|
||||
file: build/linux/alpine.Dockerfile
|
||||
platforms: linux/amd64
|
||||
push: true
|
||||
tags: |
|
||||
${{ env.IMAGE }}:${{ steps.ver.outputs.version }}
|
||||
${{ env.IMAGE }}:latest
|
||||
build-args: |
|
||||
GIT_COMMIT=${{ github.sha }}
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -4,7 +4,6 @@ dist
|
||||
portainer-checksum.txt
|
||||
api/cmd/portainer/portainer*
|
||||
storybook-static
|
||||
debug-storybook.log
|
||||
.tmp
|
||||
**/.vscode/settings.json
|
||||
**/.vscode/tasks.json
|
||||
@@ -19,5 +18,3 @@ api/docs
|
||||
.env
|
||||
go.work.sum
|
||||
|
||||
.vitest
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
version: '2'
|
||||
version: "2"
|
||||
linters:
|
||||
default: none
|
||||
enable:
|
||||
@@ -6,8 +6,11 @@ linters:
|
||||
settings:
|
||||
forbidigo:
|
||||
forbid:
|
||||
- pattern: ^dataservices.DataStore.(EdgeGroup|EdgeJob|EdgeStack|EndpointRelation|Endpoint|GitCredential|Registry|ResourceControl|Role|Settings|Snapshot|SSLSettings|Stack|Tag|User)$
|
||||
- pattern: ^dataservices.DataStore.(EdgeGroup|EdgeJob|EdgeStack|EndpointRelation|Endpoint|GitCredential|Registry|ResourceControl|Role|Settings|Snapshot|Stack|Tag|User)$
|
||||
msg: Use a transaction instead
|
||||
- pattern: ^(filepath|path)\.Join$
|
||||
msg: Use filesystem.JoinPaths() from github.com/portainer/portainer/api/filesystem to prevent path traversal attacks
|
||||
analyze-types: true
|
||||
exclusions:
|
||||
rules:
|
||||
- path: _test\.go
|
||||
linters:
|
||||
- forbidigo
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
version: '2'
|
||||
|
||||
run:
|
||||
allow-parallel-runners: true
|
||||
version: "2"
|
||||
linters:
|
||||
default: none
|
||||
enable:
|
||||
- gocritic
|
||||
- bodyclose
|
||||
- copyloopvar
|
||||
- depguard
|
||||
- errcheck
|
||||
- errorlint
|
||||
- forbidigo
|
||||
- govet
|
||||
@@ -22,17 +17,11 @@ linters:
|
||||
- durationcheck
|
||||
- errorlint
|
||||
- govet
|
||||
- usetesting
|
||||
- zerologlint
|
||||
- testifylint
|
||||
- modernize
|
||||
- unconvert
|
||||
- unused
|
||||
- zerologlint
|
||||
- exptostd
|
||||
settings:
|
||||
staticcheck:
|
||||
checks: ['all', '-ST1003', '-ST1005', '-ST1016', '-SA1019', '-QF1003']
|
||||
checks: ["all", "-ST1003", "-ST1005", "-ST1016", "-SA1019", "-QF1003"]
|
||||
depguard:
|
||||
rules:
|
||||
main:
|
||||
@@ -53,37 +42,6 @@ linters:
|
||||
desc: golang.org/x/crypto is not allowed because of FIPS mode
|
||||
- pkg: github.com/ProtonMail/go-crypto/openpgp
|
||||
desc: github.com/ProtonMail/go-crypto/openpgp is not allowed because of FIPS mode
|
||||
- pkg: github.com/cosi-project/runtime
|
||||
desc: github.com/cosi-project/runtime is not allowed because of FIPS mode
|
||||
- pkg: gopkg.in/yaml.v2
|
||||
desc: use go.yaml.in/yaml/v3 instead
|
||||
- pkg: gopkg.in/yaml.v3
|
||||
desc: use go.yaml.in/yaml/v3 instead
|
||||
- pkg: github.com/golang-jwt/jwt/v4
|
||||
desc: use github.com/golang-jwt/jwt/v5 instead
|
||||
- pkg: github.com/mitchellh/mapstructure
|
||||
desc: use github.com/go-viper/mapstructure/v2 instead
|
||||
- pkg: gopkg.in/alecthomas/kingpin.v2
|
||||
desc: use github.com/alecthomas/kingpin/v2 instead
|
||||
- pkg: github.com/jcmturner/gokrb5$
|
||||
desc: use github.com/jcmturner/gokrb5/v8 instead
|
||||
- pkg: github.com/gofrs/uuid
|
||||
desc: use github.com/google/uuid
|
||||
- pkg: github.com/Masterminds/semver$
|
||||
desc: use github.com/Masterminds/semver/v3
|
||||
- pkg: github.com/blang/semver
|
||||
desc: use github.com/Masterminds/semver/v3
|
||||
- pkg: github.com/coreos/go-semver
|
||||
desc: use github.com/Masterminds/semver/v3
|
||||
- pkg: github.com/hashicorp/go-version
|
||||
desc: use github.com/Masterminds/semver/v3
|
||||
gocritic:
|
||||
disable-all: true
|
||||
enabled-checks:
|
||||
- ruleguard
|
||||
settings:
|
||||
ruleguard:
|
||||
rules: './analysis/ssrf.go,./analysis/git.go'
|
||||
forbidigo:
|
||||
forbid:
|
||||
- pattern: ^tls\.Config$
|
||||
@@ -91,11 +49,9 @@ linters:
|
||||
- pattern: ^tls\.Config\.(InsecureSkipVerify|MinVersion|MaxVersion|CipherSuites|CurvePreferences)$
|
||||
msg: Do not set this field directly, use crypto.CreateTLSConfiguration() instead
|
||||
- pattern: ^object\.(Commit|Tag)\.Verify$
|
||||
msg: 'Not allowed because of FIPS mode'
|
||||
msg: "Not allowed because of FIPS mode"
|
||||
- pattern: ^(types\.SystemContext\.)?(DockerDaemonInsecureSkipTLSVerify|DockerInsecureSkipTLSVerify|OCIInsecureSkipTLSVerify)$
|
||||
msg: 'Not allowed because of FIPS mode'
|
||||
- pattern: ^git\.PlainClone(Context|WithOptions)?$
|
||||
msg: Use git.CloneContext with NewNoSymlinkFS to prevent symlink traversal attacks
|
||||
msg: "Not allowed because of FIPS mode"
|
||||
analyze-types: true
|
||||
exclusions:
|
||||
generated: lax
|
||||
@@ -103,21 +59,12 @@ linters:
|
||||
- comments
|
||||
- common-false-positives
|
||||
- legacy
|
||||
rules:
|
||||
- path: pkg/libhttp/ssrf
|
||||
linters:
|
||||
- gocritic
|
||||
text: ruleguard
|
||||
- path: pkg/libhttp/ssrf/builder\.go
|
||||
linters:
|
||||
- forbidigo
|
||||
- std-error-handling
|
||||
paths:
|
||||
- third_party$
|
||||
- builtin$
|
||||
- examples$
|
||||
formatters:
|
||||
enable:
|
||||
- gofmt
|
||||
exclusions:
|
||||
generated: lax
|
||||
paths:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#!/usr/bin/env sh
|
||||
. "$(dirname -- "$0")/_/husky.sh"
|
||||
|
||||
cd $(dirname -- "$0") && pnpm lint-staged
|
||||
cd $(dirname -- "$0") && yarn lint-staged
|
||||
@@ -1,5 +1,2 @@
|
||||
dist
|
||||
api/datastore/test_data
|
||||
coverage
|
||||
|
||||
pnpm-lock.yaml
|
||||
api/datastore/test_data
|
||||
15
.prettierrc
15
.prettierrc
@@ -5,18 +5,21 @@
|
||||
"trailingComma": "es5",
|
||||
"overrides": [
|
||||
{
|
||||
"files": ["*.html"],
|
||||
"files": [
|
||||
"*.html"
|
||||
],
|
||||
"options": {
|
||||
"parser": "angular"
|
||||
}
|
||||
},
|
||||
{
|
||||
"files": ["*.{j,t}sx", "*.ts"],
|
||||
"files": [
|
||||
"*.{j,t}sx",
|
||||
"*.ts"
|
||||
],
|
||||
"options": {
|
||||
"printWidth": 80
|
||||
}
|
||||
}
|
||||
],
|
||||
"plugins": ["prettier-plugin-tailwindcss"],
|
||||
"tailwindFunctions": ["clsx"]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,56 +1,30 @@
|
||||
// This file has been automatically migrated to valid ESM format by Storybook.
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { createRequire } from 'node:module';
|
||||
import path, { dirname } from 'path';
|
||||
|
||||
import { StorybookConfig } from '@storybook/react-webpack5';
|
||||
|
||||
import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin';
|
||||
import { Configuration } from 'webpack';
|
||||
import postcss from 'postcss';
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url);
|
||||
const __dirname = dirname(__filename);
|
||||
const require = createRequire(import.meta.url);
|
||||
|
||||
const config: StorybookConfig = {
|
||||
stories: ['../app/**/*.stories.@(ts|tsx)'],
|
||||
addons: [
|
||||
'@storybook/addon-links',
|
||||
'@storybook/addon-webpack5-compiler-swc',
|
||||
'@chromatic-com/storybook',
|
||||
'@storybook/addon-essentials',
|
||||
{
|
||||
name: '@storybook/addon-styling-webpack',
|
||||
|
||||
name: '@storybook/addon-styling',
|
||||
options: {
|
||||
rules: [
|
||||
{
|
||||
test: /\.css$/,
|
||||
sideEffects: true,
|
||||
use: [
|
||||
require.resolve('style-loader'),
|
||||
{
|
||||
loader: require.resolve('css-loader'),
|
||||
options: {
|
||||
importLoaders: 1,
|
||||
modules: {
|
||||
localIdentName: '[path][name]__[local]',
|
||||
auto: true,
|
||||
exportLocalsConvention: 'camelCaseOnly',
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
loader: require.resolve('postcss-loader'),
|
||||
options: {
|
||||
implementation: postcss,
|
||||
},
|
||||
},
|
||||
],
|
||||
cssLoaderOptions: {
|
||||
importLoaders: 1,
|
||||
modules: {
|
||||
localIdentName: '[path][name]__[local]',
|
||||
auto: true,
|
||||
exportLocalsConvention: 'camelCaseOnly',
|
||||
},
|
||||
],
|
||||
},
|
||||
postCss: {
|
||||
implementation: postcss,
|
||||
},
|
||||
},
|
||||
},
|
||||
'@storybook/addon-docs',
|
||||
],
|
||||
webpackFinal: (config) => {
|
||||
const rules = config?.module?.rules || [];
|
||||
@@ -93,7 +67,12 @@ const config: StorybookConfig = {
|
||||
...config,
|
||||
resolve: {
|
||||
...config.resolve,
|
||||
tsconfig: path.resolve(__dirname, '..', 'tsconfig.json'),
|
||||
plugins: [
|
||||
...(config.resolve?.plugins || []),
|
||||
new TsconfigPathsPlugin({
|
||||
extensions: config.resolve?.extensions,
|
||||
}),
|
||||
],
|
||||
},
|
||||
module: {
|
||||
...config.module,
|
||||
@@ -103,13 +82,12 @@ const config: StorybookConfig = {
|
||||
},
|
||||
staticDirs: ['./public'],
|
||||
typescript: {
|
||||
reactDocgen: 'react-docgen',
|
||||
reactDocgen: 'react-docgen-typescript',
|
||||
},
|
||||
framework: {
|
||||
name: '@storybook/react-webpack5',
|
||||
options: {},
|
||||
},
|
||||
docs: {},
|
||||
};
|
||||
|
||||
export default config;
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { useEffect } from 'react';
|
||||
import '../app/assets/css';
|
||||
import React from 'react';
|
||||
import { pushStateLocationPlugin, UIRouter } from '@uirouter/react';
|
||||
import { initialize as initMSW, mswLoader } from 'msw-storybook-addon';
|
||||
import { handlers } from '../app/setup-tests/server-handlers';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { Preview } from '@storybook/react-webpack5';
|
||||
|
||||
initMSW(
|
||||
{
|
||||
@@ -22,65 +21,31 @@ initMSW(
|
||||
handlers
|
||||
);
|
||||
|
||||
export const parameters = {
|
||||
actions: { argTypesRegex: '^on[A-Z].*' },
|
||||
controls: {
|
||||
matchers: {
|
||||
color: /(background|color)$/i,
|
||||
date: /Date$/,
|
||||
},
|
||||
},
|
||||
msw: {
|
||||
handlers,
|
||||
},
|
||||
};
|
||||
|
||||
const testQueryClient = new QueryClient({
|
||||
defaultOptions: { queries: { retry: false } },
|
||||
});
|
||||
|
||||
const preview: Preview = {
|
||||
globalTypes: {
|
||||
theme: {
|
||||
description: 'Portainer color theme',
|
||||
toolbar: {
|
||||
title: 'Theme',
|
||||
icon: 'paintbrush',
|
||||
items: [
|
||||
{ value: 'light', title: 'Light', icon: 'sun' },
|
||||
{ value: 'dark', title: 'Dark', icon: 'moon' },
|
||||
{ value: 'highcontrast', title: 'High Contrast', icon: 'eye' },
|
||||
],
|
||||
dynamicTitle: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
initialGlobals: {
|
||||
theme: 'light',
|
||||
},
|
||||
decorators: (Story, context) => {
|
||||
const theme = context.globals.theme;
|
||||
export const decorators = [
|
||||
(Story) => (
|
||||
<QueryClientProvider client={testQueryClient}>
|
||||
<UIRouter plugins={[pushStateLocationPlugin]}>
|
||||
<Story />
|
||||
</UIRouter>
|
||||
</QueryClientProvider>
|
||||
),
|
||||
];
|
||||
|
||||
useEffect(() => {
|
||||
if (theme === 'light') {
|
||||
document.documentElement.removeAttribute('theme');
|
||||
} else {
|
||||
document.documentElement.setAttribute('theme', theme);
|
||||
}
|
||||
}, [theme]);
|
||||
|
||||
return (
|
||||
<QueryClientProvider client={testQueryClient}>
|
||||
<UIRouter plugins={[pushStateLocationPlugin]}>
|
||||
<Story />
|
||||
</UIRouter>
|
||||
</QueryClientProvider>
|
||||
);
|
||||
},
|
||||
loaders: [mswLoader],
|
||||
parameters: {
|
||||
options: {
|
||||
storySort: {
|
||||
order: ['Design System', 'Components', '*'],
|
||||
},
|
||||
},
|
||||
controls: {
|
||||
matchers: {
|
||||
color: /(background|color)$/i,
|
||||
date: /Date$/,
|
||||
},
|
||||
},
|
||||
msw: {
|
||||
handlers,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default preview;
|
||||
export const loaders = [mswLoader];
|
||||
|
||||
@@ -2,26 +2,26 @@
|
||||
/* tslint:disable */
|
||||
|
||||
/**
|
||||
* Mock Service Worker.
|
||||
* Mock Service Worker (2.0.11).
|
||||
* @see https://github.com/mswjs/msw
|
||||
* - Please do NOT modify this file.
|
||||
* - Please do NOT serve this file on production.
|
||||
*/
|
||||
|
||||
const PACKAGE_VERSION = '2.12.10';
|
||||
const INTEGRITY_CHECKSUM = '4db4a41e972cec1b64cc569c66952d82';
|
||||
const INTEGRITY_CHECKSUM = 'c5f7f8e188b673ea4e677df7ea3c5a39';
|
||||
const IS_MOCKED_RESPONSE = Symbol('isMockedResponse');
|
||||
const activeClientIds = new Set();
|
||||
|
||||
addEventListener('install', function () {
|
||||
self.addEventListener('install', function () {
|
||||
self.skipWaiting();
|
||||
});
|
||||
|
||||
addEventListener('activate', function (event) {
|
||||
self.addEventListener('activate', function (event) {
|
||||
event.waitUntil(self.clients.claim());
|
||||
});
|
||||
|
||||
addEventListener('message', async function (event) {
|
||||
const clientId = Reflect.get(event.source || {}, 'id');
|
||||
self.addEventListener('message', async function (event) {
|
||||
const clientId = event.source.id;
|
||||
|
||||
if (!clientId || !self.clients) {
|
||||
return;
|
||||
@@ -48,10 +48,7 @@ addEventListener('message', async function (event) {
|
||||
case 'INTEGRITY_CHECK_REQUEST': {
|
||||
sendToClient(client, {
|
||||
type: 'INTEGRITY_CHECK_RESPONSE',
|
||||
payload: {
|
||||
packageVersion: PACKAGE_VERSION,
|
||||
checksum: INTEGRITY_CHECKSUM,
|
||||
},
|
||||
payload: INTEGRITY_CHECKSUM,
|
||||
});
|
||||
break;
|
||||
}
|
||||
@@ -61,16 +58,16 @@ addEventListener('message', async function (event) {
|
||||
|
||||
sendToClient(client, {
|
||||
type: 'MOCKING_ENABLED',
|
||||
payload: {
|
||||
client: {
|
||||
id: client.id,
|
||||
frameType: client.frameType,
|
||||
},
|
||||
},
|
||||
payload: true,
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
case 'MOCK_DEACTIVATE': {
|
||||
activeClientIds.delete(clientId);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'CLIENT_CLOSED': {
|
||||
activeClientIds.delete(clientId);
|
||||
|
||||
@@ -88,91 +85,72 @@ addEventListener('message', async function (event) {
|
||||
}
|
||||
});
|
||||
|
||||
addEventListener('fetch', function (event) {
|
||||
const requestInterceptedAt = Date.now();
|
||||
self.addEventListener('fetch', function (event) {
|
||||
const { request } = event;
|
||||
|
||||
// Bypass navigation requests.
|
||||
if (event.request.mode === 'navigate') {
|
||||
if (request.mode === 'navigate') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Opening the DevTools triggers the "only-if-cached" request
|
||||
// that cannot be handled by the worker. Bypass such requests.
|
||||
if (event.request.cache === 'only-if-cached' && event.request.mode !== 'same-origin') {
|
||||
if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Bypass all requests when there are no active clients.
|
||||
// Prevents the self-unregistered worked from handling requests
|
||||
// after it's been terminated (still remains active until the next reload).
|
||||
// after it's been deleted (still remains active until the next reload).
|
||||
if (activeClientIds.size === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Generate unique request ID.
|
||||
const requestId = crypto.randomUUID();
|
||||
event.respondWith(handleRequest(event, requestId, requestInterceptedAt));
|
||||
event.respondWith(handleRequest(event, requestId));
|
||||
});
|
||||
|
||||
/**
|
||||
* @param {FetchEvent} event
|
||||
* @param {string} requestId
|
||||
* @param {number} requestInterceptedAt
|
||||
*/
|
||||
async function handleRequest(event, requestId, requestInterceptedAt) {
|
||||
async function handleRequest(event, requestId) {
|
||||
const client = await resolveMainClient(event);
|
||||
const requestCloneForEvents = event.request.clone();
|
||||
const response = await getResponse(event, client, requestId, requestInterceptedAt);
|
||||
const response = await getResponse(event, client, requestId);
|
||||
|
||||
// Send back the response clone for the "response:*" life-cycle events.
|
||||
// Ensure MSW is active and ready to handle the message, otherwise
|
||||
// this message will pend indefinitely.
|
||||
if (client && activeClientIds.has(client.id)) {
|
||||
const serializedRequest = await serializeRequest(requestCloneForEvents);
|
||||
(async function () {
|
||||
const responseClone = response.clone();
|
||||
|
||||
// Clone the response so both the client and the library could consume it.
|
||||
const responseClone = response.clone();
|
||||
|
||||
sendToClient(
|
||||
client,
|
||||
{
|
||||
type: 'RESPONSE',
|
||||
payload: {
|
||||
isMockedResponse: IS_MOCKED_RESPONSE in response,
|
||||
request: {
|
||||
id: requestId,
|
||||
...serializedRequest,
|
||||
},
|
||||
response: {
|
||||
sendToClient(
|
||||
client,
|
||||
{
|
||||
type: 'RESPONSE',
|
||||
payload: {
|
||||
requestId,
|
||||
isMockedResponse: IS_MOCKED_RESPONSE in response,
|
||||
type: responseClone.type,
|
||||
status: responseClone.status,
|
||||
statusText: responseClone.statusText,
|
||||
headers: Object.fromEntries(responseClone.headers.entries()),
|
||||
body: responseClone.body,
|
||||
headers: Object.fromEntries(responseClone.headers.entries()),
|
||||
},
|
||||
},
|
||||
},
|
||||
responseClone.body ? [serializedRequest.body, responseClone.body] : []
|
||||
);
|
||||
[responseClone.body]
|
||||
);
|
||||
})();
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the main client for the given event.
|
||||
* Client that issues a request doesn't necessarily equal the client
|
||||
* that registered the worker. It's with the latter the worker should
|
||||
* communicate with during the response resolving phase.
|
||||
* @param {FetchEvent} event
|
||||
* @returns {Promise<Client | undefined>}
|
||||
*/
|
||||
// Resolve the main client for the given event.
|
||||
// Client that issues a request doesn't necessarily equal the client
|
||||
// that registered the worker. It's with the latter the worker should
|
||||
// communicate with during the response resolving phase.
|
||||
async function resolveMainClient(event) {
|
||||
const client = await self.clients.get(event.clientId);
|
||||
|
||||
if (activeClientIds.has(event.clientId)) {
|
||||
return client;
|
||||
}
|
||||
|
||||
if (client?.frameType === 'top-level') {
|
||||
return client;
|
||||
}
|
||||
@@ -193,37 +171,20 @@ async function resolveMainClient(event) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {FetchEvent} event
|
||||
* @param {Client | undefined} client
|
||||
* @param {string} requestId
|
||||
* @param {number} requestInterceptedAt
|
||||
* @returns {Promise<Response>}
|
||||
*/
|
||||
async function getResponse(event, client, requestId, requestInterceptedAt) {
|
||||
async function getResponse(event, client, requestId) {
|
||||
const { request } = event;
|
||||
|
||||
// Clone the request because it might've been already used
|
||||
// (i.e. its body has been read and sent to the client).
|
||||
const requestClone = event.request.clone();
|
||||
const requestClone = request.clone();
|
||||
|
||||
function passthrough() {
|
||||
// Cast the request headers to a new Headers instance
|
||||
// so the headers can be manipulated with.
|
||||
const headers = new Headers(requestClone.headers);
|
||||
const headers = Object.fromEntries(requestClone.headers.entries());
|
||||
|
||||
// Remove the "accept" header value that marked this request as passthrough.
|
||||
// This prevents request alteration and also keeps it compliant with the
|
||||
// user-defined CORS policies.
|
||||
const acceptHeader = headers.get('accept');
|
||||
if (acceptHeader) {
|
||||
const values = acceptHeader.split(',').map((value) => value.trim());
|
||||
const filteredValues = values.filter((value) => value !== 'msw/passthrough');
|
||||
|
||||
if (filteredValues.length > 0) {
|
||||
headers.set('accept', filteredValues.join(', '));
|
||||
} else {
|
||||
headers.delete('accept');
|
||||
}
|
||||
}
|
||||
// Remove internal MSW request header so the passthrough request
|
||||
// complies with any potential CORS preflight checks on the server.
|
||||
// Some servers forbid unknown request headers.
|
||||
delete headers['x-msw-intention'];
|
||||
|
||||
return fetch(requestClone, { headers });
|
||||
}
|
||||
@@ -241,19 +202,37 @@ async function getResponse(event, client, requestId, requestInterceptedAt) {
|
||||
return passthrough();
|
||||
}
|
||||
|
||||
// Bypass requests with the explicit bypass header.
|
||||
// Such requests can be issued by "ctx.fetch()".
|
||||
const mswIntention = request.headers.get('x-msw-intention');
|
||||
if (['bypass', 'passthrough'].includes(mswIntention)) {
|
||||
return passthrough();
|
||||
}
|
||||
|
||||
// Notify the client that a request has been intercepted.
|
||||
const serializedRequest = await serializeRequest(event.request);
|
||||
const requestBuffer = await request.arrayBuffer();
|
||||
const clientMessage = await sendToClient(
|
||||
client,
|
||||
{
|
||||
type: 'REQUEST',
|
||||
payload: {
|
||||
id: requestId,
|
||||
interceptedAt: requestInterceptedAt,
|
||||
...serializedRequest,
|
||||
url: request.url,
|
||||
mode: request.mode,
|
||||
method: request.method,
|
||||
headers: Object.fromEntries(request.headers.entries()),
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
destination: request.destination,
|
||||
integrity: request.integrity,
|
||||
redirect: request.redirect,
|
||||
referrer: request.referrer,
|
||||
referrerPolicy: request.referrerPolicy,
|
||||
body: requestBuffer,
|
||||
keepalive: request.keepalive,
|
||||
},
|
||||
},
|
||||
[serializedRequest.body]
|
||||
[requestBuffer]
|
||||
);
|
||||
|
||||
switch (clientMessage.type) {
|
||||
@@ -261,7 +240,7 @@ async function getResponse(event, client, requestId, requestInterceptedAt) {
|
||||
return respondWithMock(clientMessage.data);
|
||||
}
|
||||
|
||||
case 'PASSTHROUGH': {
|
||||
case 'MOCK_NOT_FOUND': {
|
||||
return passthrough();
|
||||
}
|
||||
}
|
||||
@@ -269,12 +248,6 @@ async function getResponse(event, client, requestId, requestInterceptedAt) {
|
||||
return passthrough();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Client} client
|
||||
* @param {any} message
|
||||
* @param {Array<Transferable>} transferrables
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
function sendToClient(client, message, transferrables = []) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const channel = new MessageChannel();
|
||||
@@ -287,15 +260,11 @@ function sendToClient(client, message, transferrables = []) {
|
||||
resolve(event.data);
|
||||
};
|
||||
|
||||
client.postMessage(message, [channel.port2, ...transferrables.filter(Boolean)]);
|
||||
client.postMessage(message, [channel.port2].concat(transferrables.filter(Boolean)));
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Response} response
|
||||
* @returns {Response}
|
||||
*/
|
||||
function respondWithMock(response) {
|
||||
async function respondWithMock(response) {
|
||||
// Setting response status code to 0 is a no-op.
|
||||
// However, when responding with a "Response.error()", the produced Response
|
||||
// instance will have status code set to 0. Since it's not possible to create
|
||||
@@ -313,24 +282,3 @@ function respondWithMock(response) {
|
||||
|
||||
return mockedResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Request} request
|
||||
*/
|
||||
async function serializeRequest(request) {
|
||||
return {
|
||||
url: request.url,
|
||||
mode: request.mode,
|
||||
method: request.method,
|
||||
headers: Object.fromEntries(request.headers.entries()),
|
||||
cache: request.cache,
|
||||
credentials: request.credentials,
|
||||
destination: request.destination,
|
||||
integrity: request.integrity,
|
||||
redirect: request.redirect,
|
||||
referrer: request.referrer,
|
||||
referrerPolicy: request.referrerPolicy,
|
||||
body: await request.arrayBuffer(),
|
||||
keepalive: request.keepalive,
|
||||
};
|
||||
}
|
||||
|
||||
68
CLAUDE.md
68
CLAUDE.md
@@ -1,68 +0,0 @@
|
||||
# Portainer Community Edition
|
||||
|
||||
Open-source container management platform with full Docker and Kubernetes support.
|
||||
|
||||
## Project Structure
|
||||
|
||||
For a detailed breakdown of frontend and backend directory layout, feature locations, and common development tasks, see [docs/guidelines/project-structure.md](../../docs/guidelines/project-structure.md).
|
||||
|
||||
## Frontend Guidelines
|
||||
|
||||
- [docs/guidelines/frontend-conventions.md](../../docs/guidelines/frontend-conventions.md) — component structure, React Query patterns, shared components, forms, theming
|
||||
- [docs/guidelines/typescript-conventions.md](../../docs/guidelines/typescript-conventions.md) — types, anti-patterns, union types, named constants
|
||||
- [docs/guidelines/frontend-unit-testing.md](../../docs/guidelines/frontend-unit-testing.md) — Vitest, React Testing Library
|
||||
|
||||
## Backend Guidelines
|
||||
|
||||
- [docs/guidelines/go-conventions.md](../../docs/guidelines/go-conventions.md) — error handling, naming, testing, code style
|
||||
- [docs/guidelines/server-architecture.md](../../docs/guidelines/server-architecture.md) — Clean Architecture layers, transactions, CE/EE sharing patterns
|
||||
- [docs/guidelines/logging.md](../../docs/guidelines/logging.md) — zerolog usage, log levels, message style
|
||||
- [docs/guidelines/backend-code-reusability.md](../../docs/guidelines/backend-code-reusability.md) — how CE and EE share backend code
|
||||
|
||||
## Package Manager
|
||||
|
||||
- **PNPM** 10+ (for frontend)
|
||||
- **Go** 1.26.1 (for backend)
|
||||
|
||||
## Build Commands
|
||||
|
||||
```bash
|
||||
# Full build
|
||||
make build # Build both client and server
|
||||
make build-client # Build React/AngularJS frontend
|
||||
make build-server # Build Go binary
|
||||
make build-image # Build Docker image
|
||||
|
||||
# Development
|
||||
make dev # Run both in dev mode
|
||||
make dev-client # Start webpack-dev-server (port 8999)
|
||||
make dev-server # Run containerized Go server
|
||||
|
||||
# Frontend
|
||||
pnpm dev # Webpack dev server
|
||||
pnpm build # Build frontend with webpack
|
||||
pnpm typecheck # Run typecheck for frontend (with tsc)
|
||||
pnpm lint # lint frontend (with eslint)
|
||||
pnpm test # test frontend (with vitest)
|
||||
pnpm format # format frontend (with prettier)
|
||||
|
||||
# Testing
|
||||
make test # All tests (backend + frontend)
|
||||
make test-server # Backend tests only
|
||||
make lint # Lint all code
|
||||
make format # Format code
|
||||
```
|
||||
|
||||
## Development Servers
|
||||
|
||||
- Frontend: http://localhost:8999
|
||||
- Backend: http://localhost:9000 (HTTP) / https://localhost:9443 (HTTPS)
|
||||
|
||||
## Local demo stand
|
||||
|
||||
To build an image from one or more feature branches and run it (e.g. to demo open
|
||||
PRs together), see [docs/dev-stand.md](docs/dev-stand.md). **Read its Gotchas
|
||||
first** — most importantly, build the image with `make build-image ENV=production`
|
||||
(without it, `build-image` ships a development client bundle that the CSP blocks,
|
||||
leaving the UI stuck forever on "Loading Portainer…"), and note that the admin
|
||||
password must be simple/special-char-free but at least 12 characters long.
|
||||
@@ -8,19 +8,19 @@ In the interest of fostering an open and welcoming environment, we as contributo
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment include:
|
||||
|
||||
- Using welcoming and inclusive language
|
||||
- Being respectful of differing viewpoints and experiences
|
||||
- Gracefully accepting constructive criticism
|
||||
- Focusing on what is best for the community
|
||||
- Showing empathy towards other community members
|
||||
* Using welcoming and inclusive language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Focusing on what is best for the community
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
- The use of sexualized language or imagery and unwelcome sexual attention or advances
|
||||
- Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
- Public or private harassment
|
||||
- Publishing others' private information, such as a physical or electronic address, without explicit permission
|
||||
- Other conduct which could reasonably be considered inappropriate in a professional setting
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic address, without explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a professional setting
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
@@ -34,7 +34,7 @@ This Code of Conduct applies both within project spaces and in public spaces whe
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at contribute@portainer.io. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at anthony.lapenna@portainer.io. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
|
||||
|
||||
|
||||
@@ -77,7 +77,7 @@ The feature request process is similar to the bug report process but has an extr
|
||||
|
||||
## Build and run Portainer locally
|
||||
|
||||
Ensure you have Docker, Node.js, pnpm, and Golang installed in the correct versions.
|
||||
Ensure you have Docker, Node.js, yarn, and Golang installed in the correct versions.
|
||||
|
||||
Install dependencies:
|
||||
|
||||
@@ -147,9 +147,7 @@ When adding a new route to an existing handler use the following as a template (
|
||||
// @router /{id} [get]
|
||||
```
|
||||
|
||||
explanation about each line can be found [here](https://github.com/swaggo/swag#api-operation)
|
||||
|
||||
After changing these annotations, regenerate the TypeScript API client and types — see [Generating API types](./README.md#generating-api-types).
|
||||
explanation about each line can be found (here)[https://github.com/swaggo/swag#api-operation]
|
||||
|
||||
## Licensing
|
||||
|
||||
|
||||
60
Makefile
60
Makefile
@@ -3,10 +3,8 @@ ENV=development
|
||||
WEBPACK_CONFIG=webpack/webpack.$(ENV).js
|
||||
TAG=local
|
||||
|
||||
SWAG=go run github.com/swaggo/swag/cmd/swag@v1.16.6
|
||||
GOTESTSUM_VERSION?=v1.13.0
|
||||
GOTESTSUM=go run gotest.tools/gotestsum@$(GOTESTSUM_VERSION)
|
||||
GOLANGCI_LINT_VERSION := $(shell cat $(shell git rev-parse --show-toplevel)/.golangci-version)
|
||||
SWAG=go run github.com/swaggo/swag/cmd/swag@v1.16.2
|
||||
GOTESTSUM=go run gotest.tools/gotestsum@latest
|
||||
|
||||
# Don't change anything below this line unless you know what you're doing
|
||||
.DEFAULT_GOAL := help
|
||||
@@ -22,7 +20,7 @@ all: tidy deps build-server build-client ## Build the client, server and downloa
|
||||
build-all: all ## Alias for the 'all' target (used by CI)
|
||||
|
||||
build-client: init-dist ## Build the client
|
||||
export NODE_ENV=$(ENV) && pnpm run build --config $(WEBPACK_CONFIG)
|
||||
export NODE_ENV=$(ENV) && yarn build --config $(WEBPACK_CONFIG)
|
||||
|
||||
build-server: init-dist ## Build the server binary
|
||||
./build/build_binary.sh "$(PLATFORM)" "$(ARCH)"
|
||||
@@ -31,17 +29,17 @@ build-image: build-all ## Build the Portainer image locally
|
||||
docker buildx build --load -t portainerci/portainer-ce:$(TAG) -f build/linux/Dockerfile .
|
||||
|
||||
build-storybook: ## Build and serve the storybook files
|
||||
pnpm run storybook:build
|
||||
yarn storybook:build
|
||||
|
||||
##@ Build dependencies
|
||||
.PHONY: deps server-deps client-deps tidy
|
||||
deps: server-deps client-deps ## Download all client and server build dependancies
|
||||
|
||||
## This is empty because the pipeline requires it but ce has no server deps
|
||||
server-deps: init-dist ## Download dependant server binaries
|
||||
@./build/download_binaries.sh $(PLATFORM) $(ARCH)
|
||||
|
||||
client-deps: ## Install client dependencies
|
||||
pnpm install
|
||||
yarn
|
||||
|
||||
tidy: ## Tidy up the go.mod file
|
||||
@go mod tidy
|
||||
@@ -57,12 +55,10 @@ clean: ## Remove all build and download artifacts
|
||||
test: test-server test-client ## Run all tests
|
||||
|
||||
test-client: ## Run client tests
|
||||
pnpm run test $(ARGS) --coverage
|
||||
|
||||
TEST_PACKAGES?=./...
|
||||
yarn test $(ARGS) --coverage
|
||||
|
||||
test-server: ## Run server tests
|
||||
$(GOTESTSUM) --format pkgname-and-test-fails --format-hide-empty-pkg --hide-summary skipped -- -cover -covermode=atomic -coverprofile=coverage.out $(TEST_PACKAGES)
|
||||
$(GOTESTSUM) --format pkgname-and-test-fails --format-hide-empty-pkg --hide-summary skipped -- -cover -covermode=atomic -coverprofile=coverage.out ./...
|
||||
|
||||
##@ Dev
|
||||
.PHONY: dev dev-client dev-server
|
||||
@@ -71,7 +67,7 @@ dev: ## Run both the client and server in development mode
|
||||
make dev-client
|
||||
|
||||
dev-client: ## Run the client in development mode
|
||||
pnpm install && pnpm run dev
|
||||
yarn dev
|
||||
|
||||
dev-server: build-server ## Run the server in development mode
|
||||
@./dev/run_container.sh
|
||||
@@ -85,31 +81,19 @@ dev-server-podman: build-server ## Run the server in development mode
|
||||
format: format-client format-server ## Format all code
|
||||
|
||||
format-client: ## Format client code
|
||||
pnpm run format
|
||||
yarn format
|
||||
|
||||
format-server: ## Format server code
|
||||
go fmt ./...
|
||||
|
||||
##@ Lint
|
||||
.PHONY: lint lint-client lint-server check-lint-version
|
||||
.PHONY: lint lint-client lint-server
|
||||
lint: lint-client lint-server ## Lint all code
|
||||
|
||||
lint-client: ## Lint client code
|
||||
pnpm run lint
|
||||
yarn lint
|
||||
|
||||
check-lint-version:
|
||||
@installed=v$$(golangci-lint --version 2>/dev/null | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1); \
|
||||
if [ "$$installed" = "v" ]; then \
|
||||
echo "ERROR: golangci-lint not found, need $(GOLANGCI_LINT_VERSION)"; \
|
||||
echo "Install: go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)"; \
|
||||
exit 1; \
|
||||
elif [ "$$installed" != "$(GOLANGCI_LINT_VERSION)" ]; then \
|
||||
echo "ERROR: golangci-lint $$installed installed, need $(GOLANGCI_LINT_VERSION)"; \
|
||||
echo "Install: go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@$(GOLANGCI_LINT_VERSION)"; \
|
||||
exit 1; \
|
||||
fi
|
||||
|
||||
lint-server: tidy check-lint-version ## Lint server code
|
||||
lint-server: tidy ## Lint server code
|
||||
golangci-lint run --timeout=10m -c .golangci.yaml
|
||||
golangci-lint run --timeout=10m --new-from-rev=HEAD~ -c .golangci-forward.yaml
|
||||
|
||||
@@ -121,23 +105,11 @@ dev-extension: build-server build-client ## Run the extension in development mod
|
||||
##@ Docs
|
||||
.PHONY: docs-build docs-validate docs-clean docs-validate-clean
|
||||
docs-build: init-dist ## Build docs
|
||||
go mod download
|
||||
cd api && $(SWAG) init -o "../dist/docs" -ot "yaml" -g ./http/handler/handler.go --parseDependency --parseInternal --parseDepth 2 -p pascalcase --markdownFiles ./ --overridesFile .swaggo
|
||||
cd api && $(SWAG) init -o "../dist/docs" -ot "yaml" -g ./http/handler/handler.go --parseDependency --parseInternal --parseDepth 2 -p pascalcase --markdownFiles ./
|
||||
|
||||
docs-validate: docs-build ## Validate docs
|
||||
pnpm swagger2openapi --warnOnly dist/docs/swagger.yaml -o dist/docs/openapi.yaml
|
||||
pnpm swagger-cli validate dist/docs/openapi.yaml
|
||||
|
||||
.PHONY: docs-serve
|
||||
docs-serve: docs-build ## Serve docs locally with Swagger UI on port 8080
|
||||
docker run -p 8080:8080 \
|
||||
-e SWAGGER_JSON=/foo/swagger.yaml \
|
||||
-v $(PWD)/dist/docs:/foo \
|
||||
swaggerapi/swagger-ui
|
||||
|
||||
.PHONY: generate-api
|
||||
generate-api: docs-validate ## Generate API client and types from OpenAPI spec
|
||||
pnpm generate-api
|
||||
yarn swagger2openapi --warnOnly dist/docs/swagger.yaml -o dist/docs/openapi.yaml
|
||||
yarn swagger-cli validate dist/docs/openapi.yaml
|
||||
|
||||
##@ Helpers
|
||||
.PHONY: help
|
||||
|
||||
28
README.md
28
README.md
@@ -44,35 +44,9 @@ You can join the Portainer Community by visiting [https://www.portainer.io/join-
|
||||
- Want to report a bug or request a feature? Please open [an issue](https://github.com/portainer/portainer/issues/new).
|
||||
- Want to help us build **_portainer_**? Follow our [contribution guidelines](https://docs.portainer.io/contribute/contribute) to build it locally and make a pull request.
|
||||
|
||||
## Generating API types
|
||||
|
||||
The frontend consumes a TypeScript API client (SDK functions and request/response types) that is generated from the Go API's Swagger annotations. Regenerate it after any API change — a new endpoint, a changed request/response shape, or a removed endpoint:
|
||||
|
||||
```bash
|
||||
make generate-api
|
||||
```
|
||||
|
||||
This runs the following pipeline:
|
||||
|
||||
```
|
||||
Go Swagger annotations
|
||||
→ dist/docs/swagger.yaml (make docs-build, via swaggo/swag)
|
||||
→ dist/docs/openapi.yaml (swagger2openapi + validation)
|
||||
→ app/react/portainer/generated-api/portainer/ (hey-api/openapi-ts)
|
||||
```
|
||||
|
||||
The generator is configured in [`openapi-ts.config.ts`](./openapi-ts.config.ts), which controls the output path, plugins, and tag filters (for example, `deprecated` endpoints and `edge_agent`-tagged routes are excluded).
|
||||
|
||||
The generated files live in `app/react/portainer/generated-api/portainer/` and must **not** be edited by hand — your changes would be overwritten on the next run. Import the generated SDK functions and types instead of writing direct HTTP calls:
|
||||
|
||||
- `@api/sdk.gen` — SDK functions
|
||||
- `@api/types.gen` — request/response types
|
||||
|
||||
See [Adding api docs](./CONTRIBUTING.md#adding-api-docs) for how to annotate handlers so they are picked up by the generator.
|
||||
|
||||
## Security
|
||||
|
||||
For information about reporting security vulnerabilities, please see our [Security Policy](SECURITY.md).
|
||||
- Here at Portainer, we believe in [responsible disclosure](https://en.wikipedia.org/wiki/Responsible_disclosure) of security issues. If you have found a security issue, please report it to <security@portainer.io>.
|
||||
|
||||
## Work for us
|
||||
|
||||
|
||||
60
SECURITY.md
60
SECURITY.md
@@ -1,60 +0,0 @@
|
||||
# Security Policy
|
||||
|
||||
## Supported Versions
|
||||
|
||||
Portainer maintains both Short-Term Support (STS) and Long-Term Support (LTS) versions in accordance with our official [Portainer Lifecycle Policy](https://docs.portainer.io/start/lifecycle).
|
||||
|
||||
| Version Type | Support Status |
|
||||
| ------------------------ | ------------------------------------------- |
|
||||
| LTS (Long-Term Support) | Supported for critical security fixes |
|
||||
| STS (Short-Term Support) | Supported until the next STS or LTS release |
|
||||
| Legacy / EOL | Not supported |
|
||||
|
||||
For a detailed breakdown of current versions and their specific End of Life (EOL) dates,
|
||||
please refer to the [Portainer Lifecycle Policy](https://docs.portainer.io/start/lifecycle).
|
||||
|
||||
## Reporting a Vulnerability
|
||||
|
||||
The Portainer team takes the security of our products seriously. If you believe you have found a security vulnerability in any Portainer-owned repository, please report it to us responsibly.
|
||||
|
||||
**Please do not report security vulnerabilities via public GitHub issues.**
|
||||
|
||||
### Disclosure Process
|
||||
|
||||
1. **Report**: You can report in one of two ways:
|
||||
|
||||
- **GitHub**: Use the **Report a vulnerability** button on the **Security** tab of this repository.
|
||||
|
||||
- **Email**: Send your findings to security@portainer.io.
|
||||
|
||||
2. **Details**: To help us verify the issue, please include:
|
||||
|
||||
- A description of the vulnerability and its potential impact.
|
||||
|
||||
- Step-by-step instructions to reproduce the issue (e.g. proof-of-concept code, scripts, or screenshots).
|
||||
|
||||
- The version of the software and the environment in which it was found.
|
||||
|
||||
3. **Acknowledge**: We will acknowledge receipt of your report and provide an initial assessment.
|
||||
|
||||
4. **Resolution**: We will work to resolve the issue as quickly as possible. We request that you do not disclose the vulnerability publicly until we have released a fix and notified affected users.
|
||||
|
||||
## Our Commitment
|
||||
|
||||
If you follow the responsible disclosure process, we will:
|
||||
|
||||
- Respond to your report in a timely manner.
|
||||
|
||||
- Provide an estimated timeline for remediation.
|
||||
|
||||
- Notify you when the vulnerability has been patched.
|
||||
|
||||
- Give credit for the discovery (if desired) once the fix is public.
|
||||
|
||||
We will make every effort to promptly address any security weaknesses. Security advisories and fixes will be published through GitHub Security Advisories and other channels as needed.
|
||||
|
||||
Thank you for helping keep Portainer and our community secure.
|
||||
|
||||
## Resources
|
||||
|
||||
- [Contributing to Portainer](https://docs.portainer.io/contribute/contribute#contributing-to-the-portainer-ce-codebase)
|
||||
@@ -1,118 +0,0 @@
|
||||
import {
|
||||
Children,
|
||||
useState,
|
||||
useEffect,
|
||||
useRef,
|
||||
useContext,
|
||||
createContext,
|
||||
ReactNode,
|
||||
} from 'react';
|
||||
|
||||
type MenuCtxType = {
|
||||
isOpen: boolean;
|
||||
setOpen: (v: boolean) => void;
|
||||
menuRef: React.RefObject<HTMLDivElement>;
|
||||
label: string;
|
||||
setLabel: (v: string) => void;
|
||||
};
|
||||
|
||||
const MenuCtx = createContext<MenuCtxType | null>(null);
|
||||
|
||||
export function Menu({ children }: { children?: ReactNode }) {
|
||||
const [isOpen, setOpen] = useState(false);
|
||||
const [label, setLabel] = useState('');
|
||||
const menuRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
function handleDocDown(e: MouseEvent) {
|
||||
const target = e.target as Node | null;
|
||||
if (
|
||||
isOpen &&
|
||||
menuRef.current &&
|
||||
target &&
|
||||
!menuRef.current.contains(target)
|
||||
) {
|
||||
setOpen(false);
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('mousedown', handleDocDown);
|
||||
return () => document.removeEventListener('mousedown', handleDocDown);
|
||||
}, [isOpen]);
|
||||
|
||||
return (
|
||||
<MenuCtx.Provider value={{ isOpen, setOpen, menuRef, label, setLabel }}>
|
||||
<div ref={menuRef}>{children}</div>
|
||||
</MenuCtx.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function MenuButton({
|
||||
children,
|
||||
onClick: externalOnClick,
|
||||
...props
|
||||
}: {
|
||||
children?: ReactNode;
|
||||
onClick?: () => void;
|
||||
[key: string]: unknown;
|
||||
}) {
|
||||
const ctx = useContext(MenuCtx);
|
||||
|
||||
useEffect(() => {
|
||||
const firstText = Children.toArray(children).find(
|
||||
(c) => typeof c === 'string'
|
||||
);
|
||||
if (firstText) ctx?.setLabel(firstText as string);
|
||||
});
|
||||
|
||||
function handleClick() {
|
||||
externalOnClick?.();
|
||||
ctx?.setOpen(!ctx.isOpen);
|
||||
}
|
||||
|
||||
return (
|
||||
<button type="button" onClick={handleClick} {...props}>
|
||||
{children}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
export function MenuList({
|
||||
children,
|
||||
className,
|
||||
}: {
|
||||
children?: ReactNode;
|
||||
className?: string;
|
||||
}) {
|
||||
const ctx = useContext(MenuCtx);
|
||||
if (!ctx?.isOpen) return null;
|
||||
return (
|
||||
<div role="menu" aria-label={ctx.label || undefined} className={className}>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function MenuItem({
|
||||
children,
|
||||
onSelect,
|
||||
className,
|
||||
}: {
|
||||
children?: ReactNode;
|
||||
onSelect?: () => void;
|
||||
className?: string;
|
||||
}) {
|
||||
const ctx = useContext(MenuCtx);
|
||||
|
||||
function handleClick() {
|
||||
onSelect?.();
|
||||
ctx?.setOpen(false);
|
||||
}
|
||||
|
||||
return (
|
||||
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/interactive-supports-focus
|
||||
<div role="menuitem" onClick={handleClick} className={className}>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
//go:build ignore
|
||||
|
||||
package gorules
|
||||
|
||||
import "github.com/quasilyte/go-ruleguard/dsl"
|
||||
|
||||
// inMemoryCloneWithWorktree flags git clone calls that use memory.NewStorage() as
|
||||
// the storer while also writing files to a real worktree. This holds all git objects
|
||||
// in heap for the duration of the clone, which is unbounded for user-supplied repos.
|
||||
func inMemoryCloneWithWorktree(m dsl.Matcher) {
|
||||
m.Match(`git.CloneContext($_, memory.NewStorage(), $wt, $_)`).
|
||||
Where(m["wt"].Text != "nil").
|
||||
Report(`git.CloneContext with memory.NewStorage() holds all git objects in heap; use gogitfs.NewStorage with a filesystem storer instead`)
|
||||
|
||||
m.Match(`git.Clone(memory.NewStorage(), $wt, $_)`).
|
||||
Where(m["wt"].Text != "nil").
|
||||
Report(`git.Clone with memory.NewStorage() holds all git objects in heap; use gogitfs.NewStorage with a filesystem storer instead`)
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
//go:build ignore
|
||||
|
||||
package gorules
|
||||
|
||||
import "github.com/quasilyte/go-ruleguard/dsl"
|
||||
|
||||
// unwrappedHTTPTransport flags any bare http.Transport composite literal.
|
||||
// All transports must be created via ssrf.NewTransport or ssrf.NewInternalTransport,
|
||||
// which clone http.DefaultTransport and handle SSRF protection internally.
|
||||
func unwrappedHTTPTransport(m dsl.Matcher) {
|
||||
m.Match(`$f(&http.Transport{$*_})`).
|
||||
Report(`$f receives a bare *http.Transport; use ssrf.NewTransport(tlsConfig) or ssrf.NewInternalTransport(tlsConfig) instead`)
|
||||
|
||||
m.Match(`$_ := &http.Transport{$*_}`).
|
||||
Report(`bare *http.Transport variable; use ssrf.NewTransport(tlsConfig) or ssrf.NewInternalTransport(tlsConfig) instead`)
|
||||
|
||||
m.Match(`$_.Transport = &http.Transport{$*_}`).
|
||||
Report(`bare *http.Transport field assignment; use ssrf.NewTransport(tlsConfig) or ssrf.NewInternalTransport(tlsConfig) instead`)
|
||||
}
|
||||
|
||||
// helmGetterTransport flags getter.WithTransport calls that receive a bare *http.Transport.
|
||||
// Helm v4 installs its own transport and bypasses http.DefaultTransport, so the transport
|
||||
// passed here must be created via ssrf.NewTransport.
|
||||
func helmGetterTransport(m dsl.Matcher) {
|
||||
m.Match(`getter.WithTransport(&http.Transport{$*_})`).
|
||||
Report(`getter.WithTransport called with a bare *http.Transport; use ssrf.NewTransport(tlsConfig) as Helm v4 bypasses http.DefaultTransport`)
|
||||
}
|
||||
|
||||
// cloneDefaultTransport flags direct clones of *http.Transport outside main.go.
|
||||
// The one legitimate clone is in main.go where http.DefaultTransport is globally
|
||||
// wrapped with SSRF protection at server startup.
|
||||
func cloneDefaultTransport(m dsl.Matcher) {
|
||||
m.Match(`$_.(*http.Transport).Clone()`).
|
||||
Where(!m.File().Name.Matches(`^main\.go$`)).
|
||||
Report(`cloning *http.Transport directly is forbidden; use ssrf.NewTransport(tlsConfig) or ssrf.NewInternalTransport(tlsConfig) instead`)
|
||||
}
|
||||
|
||||
// internalTransportMisuse flags calls to NewInternalTransport outside the proxy
|
||||
// factory files where Chisel-tunnel and in-cluster K8s destinations are valid exemptions.
|
||||
func internalTransportMisuse(m dsl.Matcher) {
|
||||
m.Match(`ssrf.NewInternalTransport($*_)`).
|
||||
Where(
|
||||
!(m.File().PkgPath.Matches(`proxy/factory`) &&
|
||||
m.File().Name.Matches(`^(docker|agent|local_transport|edge_transport|docker_unix|docker_windows)\.go$`))).
|
||||
Report(`NewInternalTransport bypasses SSRF validation; only valid in the proxy factory files for local sockets and internally-routed endpoints`)
|
||||
}
|
||||
|
||||
// dialerOverride flags direct assignments to any of the dialer fields on a transport.
|
||||
// The only valid assignments are in docker_unix.go and docker_windows.go where a
|
||||
// custom dialer is required for unix sockets and named pipes.
|
||||
func dialerOverride(m dsl.Matcher) {
|
||||
m.Match(`$_.DialContext = $*_`).
|
||||
Where(
|
||||
!(m.File().PkgPath.Matches(`proxy/factory`) &&
|
||||
m.File().Name.Matches(`^(docker_unix|docker_windows)\.go$`))).
|
||||
Report(`direct DialContext assignment replaces the transport dialer; use ssrf.NewTransport or ssrf.NewInternalTransport instead`)
|
||||
|
||||
m.Match(`$_.Dial = $*_`).
|
||||
Where(
|
||||
!(m.File().PkgPath.Matches(`proxy/factory`) &&
|
||||
m.File().Name.Matches(`^(docker_unix|docker_windows)\.go$`))).
|
||||
Report(`direct Dial assignment replaces the transport dialer; use ssrf.NewTransport or ssrf.NewInternalTransport instead`)
|
||||
|
||||
m.Match(`$_.DialTLSContext = $*_`).
|
||||
Where(
|
||||
!(m.File().PkgPath.Matches(`proxy/factory`) &&
|
||||
m.File().Name.Matches(`^(docker_unix|docker_windows)\.go$`))).
|
||||
Report(`direct DialTLSContext assignment replaces the transport dialer; use ssrf.NewTransport or ssrf.NewInternalTransport instead`)
|
||||
|
||||
m.Match(`$_.DialTLS = $*_`).
|
||||
Where(
|
||||
!(m.File().PkgPath.Matches(`proxy/factory`) &&
|
||||
m.File().Name.Matches(`^(docker_unix|docker_windows)\.go$`))).
|
||||
Report(`direct DialTLS assignment replaces the transport dialer; use ssrf.NewTransport or ssrf.NewInternalTransport instead`)
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
//go:build tools
|
||||
|
||||
package gorules
|
||||
|
||||
import _ "github.com/quasilyte/go-ruleguard/dsl"
|
||||
@@ -1 +0,0 @@
|
||||
replace k8s.io/apimachinery/pkg/apis/meta/v1.Duration string
|
||||
@@ -19,22 +19,24 @@ const RedirectReasonAdminInitTimeout string = "AdminInitTimeout"
|
||||
type Monitor struct {
|
||||
timeout time.Duration
|
||||
datastore dataservices.DataStore
|
||||
shutdownCtx context.Context
|
||||
cancellationFunc context.CancelFunc
|
||||
mu sync.RWMutex
|
||||
adminInitDisabled bool
|
||||
}
|
||||
|
||||
// New creates a monitor that when started will wait for the timeout duration and then shutdown the application unless it has been initialized.
|
||||
func New(timeout time.Duration, datastore dataservices.DataStore) *Monitor {
|
||||
func New(timeout time.Duration, datastore dataservices.DataStore, shutdownCtx context.Context) *Monitor {
|
||||
return &Monitor{
|
||||
timeout: timeout,
|
||||
datastore: datastore,
|
||||
shutdownCtx: shutdownCtx,
|
||||
adminInitDisabled: false,
|
||||
}
|
||||
}
|
||||
|
||||
// Start starts the monitor. The monitor will stop when ctx is cancelled, or when Stop is called.
|
||||
func (m *Monitor) Start(ctx context.Context) {
|
||||
// Starts starts the monitor. Active monitor could be stopped or shuttted down by cancelling the shutdown context.
|
||||
func (m *Monitor) Start() {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
@@ -42,7 +44,7 @@ func (m *Monitor) Start(ctx context.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
cancellationCtx, cancellationFunc := context.WithCancel(ctx)
|
||||
cancellationCtx, cancellationFunc := context.WithCancel(context.Background())
|
||||
m.cancellationFunc = cancellationFunc
|
||||
|
||||
go func() {
|
||||
@@ -67,6 +69,8 @@ func (m *Monitor) Start(ctx context.Context) {
|
||||
}
|
||||
case <-cancellationCtx.Done():
|
||||
log.Debug().Msg("canceling initialization monitor")
|
||||
case <-m.shutdownCtx.Done():
|
||||
log.Debug().Msg("shutting down initialization monitor")
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package adminmonitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
@@ -11,28 +11,21 @@ import (
|
||||
)
|
||||
|
||||
func Test_stopWithoutStarting(t *testing.T) {
|
||||
t.Parallel()
|
||||
monitor := New(1*time.Minute, nil)
|
||||
monitor := New(1*time.Minute, nil, nil)
|
||||
monitor.Stop()
|
||||
}
|
||||
|
||||
func Test_stopCouldBeCalledMultipleTimes(t *testing.T) {
|
||||
t.Parallel()
|
||||
monitor := New(1*time.Minute, nil)
|
||||
monitor := New(1*time.Minute, nil, nil)
|
||||
monitor.Stop()
|
||||
monitor.Stop()
|
||||
}
|
||||
|
||||
func Test_startOrStopCouldBeCalledMultipleTimesConcurrently(t *testing.T) {
|
||||
t.Parallel()
|
||||
synctest.Test(t, test_startOrStopCouldBeCalledMultipleTimesConcurrently)
|
||||
}
|
||||
monitor := New(1*time.Minute, nil, context.Background())
|
||||
|
||||
func test_startOrStopCouldBeCalledMultipleTimesConcurrently(t *testing.T) {
|
||||
monitor := New(1*time.Minute, nil)
|
||||
|
||||
go monitor.Start(t.Context())
|
||||
monitor.Start(t.Context())
|
||||
go monitor.Start()
|
||||
monitor.Start()
|
||||
|
||||
go monitor.Stop()
|
||||
monitor.Stop()
|
||||
@@ -41,9 +34,8 @@ func test_startOrStopCouldBeCalledMultipleTimesConcurrently(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_canStopStartedMonitor(t *testing.T) {
|
||||
t.Parallel()
|
||||
monitor := New(1*time.Minute, nil)
|
||||
monitor.Start(t.Context())
|
||||
monitor := New(1*time.Minute, nil, context.Background())
|
||||
monitor.Start()
|
||||
assert.NotNil(t, monitor.cancellationFunc, "cancellation function is missing in started monitor")
|
||||
|
||||
monitor.Stop()
|
||||
@@ -51,12 +43,11 @@ func Test_canStopStartedMonitor(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_start_shouldDisableInstanceAfterTimeout_ifNotInitialized(t *testing.T) {
|
||||
t.Parallel()
|
||||
timeout := 10 * time.Millisecond
|
||||
|
||||
datastore := i.NewDatastore(i.WithUsers([]portainer.User{}))
|
||||
monitor := New(timeout, datastore)
|
||||
monitor.Start(t.Context())
|
||||
monitor := New(timeout, datastore, context.Background())
|
||||
monitor.Start()
|
||||
|
||||
<-time.After(20 * timeout)
|
||||
assert.True(t, monitor.WasInstanceDisabled(), "monitor should have been timeout and instance is disabled")
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -12,23 +11,20 @@ import (
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/url"
|
||||
"github.com/portainer/portainer/pkg/libhttp/ssrf"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// GetAgentVersionAndPlatform returns the agent version and platform
|
||||
//
|
||||
// it sends a ping to the agent and parses the version and platform from the headers
|
||||
func GetAgentVersionAndPlatform(endpointUrl string, tlsConfig *tls.Config) (portainer.AgentPlatform, string, error) { //nolint:forbidigo
|
||||
if err := ssrf.CheckURL(context.Background(), endpointUrl); err != nil {
|
||||
return 0, "", err
|
||||
httpCli := &http.Client{
|
||||
Timeout: 3 * time.Second,
|
||||
}
|
||||
|
||||
httpCli := &http.Client{Timeout: 3 * time.Second}
|
||||
|
||||
if tlsConfig != nil {
|
||||
httpCli.Transport = ssrf.NewTransport(tlsConfig)
|
||||
httpCli.Transport = &http.Transport{
|
||||
TLSClientConfig: tlsConfig,
|
||||
}
|
||||
}
|
||||
|
||||
parsedURL, err := url.ParseURL(endpointUrl + "/ping")
|
||||
@@ -48,10 +44,8 @@ func GetAgentVersionAndPlatform(endpointUrl string, tlsConfig *tls.Config) (port
|
||||
return 0, "", err
|
||||
}
|
||||
|
||||
_, _ = io.Copy(io.Discard, resp.Body)
|
||||
if err := resp.Body.Close(); err != nil {
|
||||
log.Warn().Err(err).Msg("failed to close response body")
|
||||
}
|
||||
io.Copy(io.Discard, resp.Body)
|
||||
resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusNoContent {
|
||||
return 0, "", fmt.Errorf("Failed request with status %d", resp.StatusCode)
|
||||
|
||||
@@ -1,119 +0,0 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func tlsServer(t *testing.T, handler http.HandlerFunc) *httptest.Server {
|
||||
t.Helper()
|
||||
srv := httptest.NewTLSServer(handler)
|
||||
t.Cleanup(srv.Close)
|
||||
|
||||
return srv
|
||||
}
|
||||
|
||||
func TestGetAgentVersionAndPlatform_Success(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srv := tlsServer(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set(portainer.PortainerAgentHeader, "2.19.0")
|
||||
w.Header().Set(portainer.HTTPResponseAgentPlatform, "1")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
})
|
||||
|
||||
tlsCfg := srv.Client().Transport.(*http.Transport).TLSClientConfig
|
||||
platform, version, err := GetAgentVersionAndPlatform(srv.URL, tlsCfg)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, portainer.AgentPlatformDocker, platform)
|
||||
require.Equal(t, "2.19.0", version)
|
||||
}
|
||||
|
||||
func TestGetAgentVersionAndPlatform_NonOKStatus(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srv := tlsServer(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
})
|
||||
|
||||
tlsCfg := srv.Client().Transport.(*http.Transport).TLSClientConfig
|
||||
_, _, err := GetAgentVersionAndPlatform(srv.URL, tlsCfg)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestGetAgentVersionAndPlatform_MissingVersionHeader(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srv := tlsServer(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set(portainer.HTTPResponseAgentPlatform, "1")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
})
|
||||
|
||||
tlsCfg := srv.Client().Transport.(*http.Transport).TLSClientConfig
|
||||
_, _, err := GetAgentVersionAndPlatform(srv.URL, tlsCfg)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestGetAgentVersionAndPlatform_MissingPlatformHeader(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srv := tlsServer(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set(portainer.PortainerAgentHeader, "2.19.0")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
})
|
||||
|
||||
tlsCfg := srv.Client().Transport.(*http.Transport).TLSClientConfig
|
||||
_, _, err := GetAgentVersionAndPlatform(srv.URL, tlsCfg)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestGetAgentVersionAndPlatform_InvalidPlatformZero(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srv := tlsServer(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set(portainer.PortainerAgentHeader, "2.19.0")
|
||||
w.Header().Set(portainer.HTTPResponseAgentPlatform, "0")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
})
|
||||
|
||||
tlsCfg := srv.Client().Transport.(*http.Transport).TLSClientConfig
|
||||
_, _, err := GetAgentVersionAndPlatform(srv.URL, tlsCfg)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestGetAgentVersionAndPlatform_NonNumericPlatform(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
srv := tlsServer(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set(portainer.PortainerAgentHeader, "2.19.0")
|
||||
w.Header().Set(portainer.HTTPResponseAgentPlatform, "docker")
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
})
|
||||
|
||||
tlsCfg := srv.Client().Transport.(*http.Transport).TLSClientConfig
|
||||
_, _, err := GetAgentVersionAndPlatform(srv.URL, tlsCfg)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestGetAgentVersionAndPlatform_PingPathAppended(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var gotPath string
|
||||
srv := tlsServer(t, func(w http.ResponseWriter, r *http.Request) {
|
||||
gotPath = r.URL.Path
|
||||
w.Header().Set(portainer.PortainerAgentHeader, "2.19.0")
|
||||
w.Header().Set(portainer.HTTPResponseAgentPlatform, strconv.Itoa(int(portainer.AgentPlatformKubernetes)))
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
})
|
||||
|
||||
tlsCfg := srv.Client().Transport.(*http.Transport).TLSClientConfig
|
||||
_, _, err := GetAgentVersionAndPlatform(srv.URL, tlsCfg)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "/ping", gotPath)
|
||||
}
|
||||
64
api/api-description.md
Normal file
64
api/api-description.md
Normal file
@@ -0,0 +1,64 @@
|
||||
Portainer API is an HTTP API served by Portainer. It is used by the Portainer UI and everything you can do with the UI can be done using the HTTP API.
|
||||
Examples are available at https://documentation.portainer.io/api/api-examples/
|
||||
You can find out more about Portainer at [http://portainer.io](http://portainer.io) and get some support on [Slack](http://portainer.io/slack/).
|
||||
|
||||
# Authentication
|
||||
|
||||
Most of the API environments(endpoints) require to be authenticated as well as some level of authorization to be used.
|
||||
Portainer API uses JSON Web Token to manage authentication and thus requires you to provide a token in the **Authorization** header of each request
|
||||
with the **Bearer** authentication mechanism.
|
||||
|
||||
Example:
|
||||
|
||||
```
|
||||
Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE
|
||||
```
|
||||
|
||||
# Security
|
||||
|
||||
Each API environment(endpoint) has an associated access policy, it is documented in the description of each environment(endpoint).
|
||||
|
||||
Different access policies are available:
|
||||
|
||||
- Public access
|
||||
- Authenticated access
|
||||
- Restricted access
|
||||
- Administrator access
|
||||
|
||||
### Public access
|
||||
|
||||
No authentication is required to access the environments(endpoints) with this access policy.
|
||||
|
||||
### Authenticated access
|
||||
|
||||
Authentication is required to access the environments(endpoints) with this access policy.
|
||||
|
||||
### Restricted access
|
||||
|
||||
Authentication is required to access the environments(endpoints) with this access policy.
|
||||
Extra-checks might be added to ensure access to the resource is granted. Returned data might also be filtered.
|
||||
|
||||
### Administrator access
|
||||
|
||||
Authentication as well as an administrator role are required to access the environments(endpoints) with this access policy.
|
||||
|
||||
# Execute Docker requests
|
||||
|
||||
Portainer **DO NOT** expose specific environments(endpoints) to manage your Docker resources (create a container, remove a volume, etc...).
|
||||
|
||||
Instead, it acts as a reverse-proxy to the Docker HTTP API. This means that you can execute Docker requests **via** the Portainer HTTP API.
|
||||
|
||||
To do so, you can use the `/endpoints/{id}/docker` Portainer API environment(endpoint) (which is not documented below due to Swagger limitations). This environment(endpoint) has a restricted access policy so you still need to be authenticated to be able to query this environment(endpoint). Any query on this environment(endpoint) will be proxied to the Docker API of the associated environment(endpoint) (requests and responses objects are the same as documented in the Docker API).
|
||||
|
||||
# Private Registry
|
||||
|
||||
Using private registry, you will need to pass a based64 encoded JSON string ‘{"registryId":\<registryID value\>}’ inside the Request Header. The parameter name is "X-Registry-Auth".
|
||||
\<registryID value\> - The registry ID where the repository was created.
|
||||
|
||||
Example:
|
||||
|
||||
```
|
||||
eyJyZWdpc3RyeUlkIjoxfQ==
|
||||
```
|
||||
|
||||
**NOTE**: You can find more information on how to query the Docker API in the [Docker official documentation](https://docs.docker.com/engine/api/v1.30/) as well as in [this Portainer example](https://documentation.portainer.io/api/api-examples/).
|
||||
61
api/api.md
61
api/api.md
@@ -1,61 +0,0 @@
|
||||
The Portainer API is an HTTP API served by Portainer. It is used by the Portainer UI, and anything you can do in the UI can also be done via the HTTP API.
|
||||
|
||||
API examples are available in the [Portainer documentation](https://documentation.portainer.io/api/api-examples/)
|
||||
|
||||
You can find out more about Portainer [on our website](http://portainer.io) and get some support on [Slack](http://portainer.io/slack/).
|
||||
|
||||
# Authentication
|
||||
|
||||
Most of the API endpoints require authentication, as well as some level of authorization.
|
||||
Portainer uses JSON Web Tokens to manage authentication. You must provide a token in the **Authorization** header of each request using the **Bearer** scheme.
|
||||
|
||||
Example:
|
||||
|
||||
```
|
||||
Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsInJvbGUiOjEsImV4cCI6MTQ5OTM3NjE1NH0.NJ6vE8FY1WG6jsRQzfMqeatJ4vh2TWAeeYfDhP71YEE
|
||||
```
|
||||
|
||||
# Security
|
||||
|
||||
Each API endpoint has an associated access policy, documented in its description.
|
||||
|
||||
The following policies are available:
|
||||
|
||||
- Public access
|
||||
- Authenticated access
|
||||
- Restricted access
|
||||
- Administrator access
|
||||
|
||||
### Public access
|
||||
|
||||
No authentication is required.
|
||||
|
||||
### Authenticated access
|
||||
|
||||
Authentication is required.
|
||||
|
||||
### Restricted access
|
||||
|
||||
Authentication is required. Additional checks may apply to verify access to the resource, and returned data may be filtered.
|
||||
|
||||
### Administrator access
|
||||
|
||||
Authentication and an administrator role are both required.
|
||||
|
||||
# Execute Docker requests
|
||||
|
||||
Portainer does not expose dedicated endpoints for managing Docker resources (create a container, remove a volume, etc).
|
||||
|
||||
Instead, it acts as a reverse-proxy to the Docker HTTP API, allowing you to execute Docker requests via the Portainer HTTP API.
|
||||
|
||||
To do so, use the `/endpoints/{id}/docker` endpoint. Note that this endpoint is not documented below due to Swagger limitations. It has a restricted access policy, so authentication is still required. Any request made to this endpoint is proxied to the Docker API of the associated environment - request and response objects are identical to those in the [Docker official documentation](https://docs.docker.com/engine/api).
|
||||
|
||||
# Private Registry
|
||||
|
||||
When using a private registry, include a Base64-encoded JSON string in the request header. The header parameter name is `X-Registry-Auth` and the value should encode the following structure: ‘{"registryId":\<registryId\>}’ where `<registryId>` is the ID of the registry where the repository was created.
|
||||
|
||||
Example encoded value:
|
||||
|
||||
```
|
||||
eyJyZWdpc3RyeUlkIjoxfQ==
|
||||
```
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
)
|
||||
|
||||
func Test_generateRandomKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
tests := []struct {
|
||||
|
||||
@@ -71,7 +71,7 @@ func (c *ApiKeyCache[T]) InvalidateUserKeyCache(userId portainer.UserID) bool {
|
||||
for _, k := range c.cache.Keys() {
|
||||
user, _, _ := c.Get(k.(string))
|
||||
if c.userCmpFn(user, userId) {
|
||||
present = c.cache.Remove(k) || present
|
||||
present = c.cache.Remove(k)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
)
|
||||
|
||||
func Test_apiKeyCacheGet(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
keyCache := NewAPIKeyCache(10, compareUser)
|
||||
@@ -44,7 +43,6 @@ func Test_apiKeyCacheGet(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_apiKeyCacheSet(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
keyCache := NewAPIKeyCache(10, compareUser)
|
||||
@@ -70,7 +68,6 @@ func Test_apiKeyCacheSet(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_apiKeyCacheDelete(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
keyCache := NewAPIKeyCache(10, compareUser)
|
||||
@@ -90,7 +87,6 @@ func Test_apiKeyCacheDelete(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_apiKeyCacheLRU(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
tests := []struct {
|
||||
@@ -152,7 +148,6 @@ func Test_apiKeyCacheLRU(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_apiKeyCacheInvalidateUserKeyCache(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
keyCache := NewAPIKeyCache(10, compareUser)
|
||||
|
||||
@@ -17,13 +17,11 @@ import (
|
||||
)
|
||||
|
||||
func Test_SatisfiesAPIKeyServiceInterface(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
is.Implements((*APIKeyService)(nil), NewAPIKeyService(nil, nil))
|
||||
}
|
||||
|
||||
func Test_GenerateApiKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
_, store := datastore.MustNewTestStore(t, true, true)
|
||||
@@ -77,7 +75,6 @@ func Test_GenerateApiKey(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_GetAPIKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
_, store := datastore.MustNewTestStore(t, true, true)
|
||||
@@ -97,7 +94,6 @@ func Test_GetAPIKey(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_GetAPIKeys(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
_, store := datastore.MustNewTestStore(t, true, true)
|
||||
@@ -118,7 +114,6 @@ func Test_GetAPIKeys(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_GetDigestUserAndKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
_, store := datastore.MustNewTestStore(t, true, true)
|
||||
@@ -154,7 +149,6 @@ func Test_GetDigestUserAndKey(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_UpdateAPIKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
_, store := datastore.MustNewTestStore(t, true, true)
|
||||
@@ -163,10 +157,7 @@ func Test_UpdateAPIKey(t *testing.T) {
|
||||
|
||||
t.Run("Successfully updates the api-key LastUsed time", func(t *testing.T) {
|
||||
user := portainer.User{ID: 1}
|
||||
|
||||
err := store.User().Create(&user)
|
||||
require.NoError(t, err)
|
||||
|
||||
store.User().Create(&user)
|
||||
_, apiKey, err := service.GenerateApiKey(user, "test-x")
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -203,7 +194,6 @@ func Test_UpdateAPIKey(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_DeleteAPIKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
_, store := datastore.MustNewTestStore(t, true, true)
|
||||
@@ -244,7 +234,6 @@ func Test_DeleteAPIKey(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_InvalidateUserKeyCache(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
_, store := datastore.MustNewTestStore(t, true, true)
|
||||
|
||||
@@ -17,15 +17,18 @@ func TarFileInBuffer(fileContent []byte, fileName string, mode int64) ([]byte, e
|
||||
Size: int64(len(fileContent)),
|
||||
}
|
||||
|
||||
if err := tarWriter.WriteHeader(header); err != nil {
|
||||
err := tarWriter.WriteHeader(header)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err := tarWriter.Write(fileContent); err != nil {
|
||||
_, err = tarWriter.Write(fileContent)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := tarWriter.Close(); err != nil {
|
||||
err = tarWriter.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -40,7 +43,10 @@ type tarFileInBuffer struct {
|
||||
|
||||
func NewTarFileInBuffer() *tarFileInBuffer {
|
||||
var b bytes.Buffer
|
||||
return &tarFileInBuffer{b: &b, w: tar.NewWriter(&b)}
|
||||
return &tarFileInBuffer{
|
||||
b: &b,
|
||||
w: tar.NewWriter(&b),
|
||||
}
|
||||
}
|
||||
|
||||
// Put puts a single file to tar archive buffer.
|
||||
@@ -55,9 +61,11 @@ func (t *tarFileInBuffer) Put(fileContent []byte, fileName string, mode int64) e
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := t.w.Write(fileContent)
|
||||
if _, err := t.w.Write(fileContent); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
|
||||
// Bytes returns the archive as a byte array.
|
||||
|
||||
@@ -9,9 +9,6 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/portainer/portainer/api/filesystem"
|
||||
"github.com/portainer/portainer/api/logs"
|
||||
)
|
||||
|
||||
// TarGzDir creates a tar.gz archive and returns it's path.
|
||||
@@ -23,13 +20,12 @@ func TarGzDir(absolutePath string) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer logs.CloseAndLogErr(outFile)
|
||||
defer outFile.Close()
|
||||
|
||||
zipWriter := gzip.NewWriter(outFile)
|
||||
defer logs.CloseAndLogErr(zipWriter)
|
||||
|
||||
defer zipWriter.Close()
|
||||
tarWriter := tar.NewWriter(zipWriter)
|
||||
defer logs.CloseAndLogErr(tarWriter)
|
||||
defer tarWriter.Close()
|
||||
|
||||
err = filepath.Walk(absolutePath, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
@@ -90,7 +86,7 @@ func ExtractTarGz(r io.Reader, outputDirPath string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer logs.CloseAndLogErr(zipReader)
|
||||
defer zipReader.Close()
|
||||
|
||||
tarReader := tar.NewReader(zipReader)
|
||||
|
||||
@@ -109,7 +105,7 @@ func ExtractTarGz(r io.Reader, outputDirPath string) error {
|
||||
case tar.TypeDir:
|
||||
// skip, dir will be created with a file
|
||||
case tar.TypeReg:
|
||||
p := filesystem.JoinPaths(outputDirPath, header.Name)
|
||||
p := filepath.Clean(filepath.Join(outputDirPath, header.Name))
|
||||
if err := os.MkdirAll(filepath.Dir(p), 0o744); err != nil {
|
||||
return fmt.Errorf("Failed to extract dir %s", filepath.Dir(p))
|
||||
}
|
||||
@@ -120,7 +116,7 @@ func ExtractTarGz(r io.Reader, outputDirPath string) error {
|
||||
if _, err := io.Copy(outFile, tarReader); err != nil {
|
||||
return fmt.Errorf("Failed to extract file %s", header.Name)
|
||||
}
|
||||
logs.CloseAndLogErr(outFile)
|
||||
outFile.Close()
|
||||
default:
|
||||
return fmt.Errorf("tar: unknown type: %v in %s",
|
||||
header.Typeflag,
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
package archive
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"compress/gzip"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/portainer/portainer/api/filesystem"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -17,7 +14,7 @@ import (
|
||||
func listFiles(dir string) []string {
|
||||
items := make([]string, 0)
|
||||
|
||||
if err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
||||
filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
|
||||
if path == dir {
|
||||
return nil
|
||||
}
|
||||
@@ -25,33 +22,30 @@ func listFiles(dir string) []string {
|
||||
items = append(items, path)
|
||||
|
||||
return nil
|
||||
}); err != nil {
|
||||
log.Warn().Err(err).Msg("failed to list files in directory")
|
||||
}
|
||||
})
|
||||
|
||||
return items
|
||||
}
|
||||
|
||||
func Test_shouldCreateArchive(t *testing.T) {
|
||||
t.Parallel()
|
||||
tmpdir := t.TempDir()
|
||||
content := []byte("content")
|
||||
|
||||
err := os.WriteFile(filesystem.JoinPaths(tmpdir, "outer"), content, 0600)
|
||||
err := os.WriteFile(path.Join(tmpdir, "outer"), content, 0600)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = os.MkdirAll(filesystem.JoinPaths(tmpdir, "dir"), 0700)
|
||||
os.MkdirAll(path.Join(tmpdir, "dir"), 0700)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = os.WriteFile(filesystem.JoinPaths(tmpdir, "dir", ".dotfile"), content, 0600)
|
||||
err = os.WriteFile(path.Join(tmpdir, "dir", ".dotfile"), content, 0600)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = os.WriteFile(filesystem.JoinPaths(tmpdir, "dir", "inner"), content, 0600)
|
||||
err = os.WriteFile(path.Join(tmpdir, "dir", "inner"), content, 0600)
|
||||
require.NoError(t, err)
|
||||
|
||||
gzPath, err := TarGzDir(tmpdir)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, filesystem.JoinPaths(tmpdir, filepath.Base(tmpdir)+".tar.gz"), gzPath)
|
||||
assert.Equal(t, filepath.Join(tmpdir, filepath.Base(tmpdir)+".tar.gz"), gzPath)
|
||||
|
||||
extractionDir := t.TempDir()
|
||||
cmd := exec.Command("tar", "-xzf", gzPath, "-C", extractionDir)
|
||||
@@ -61,7 +55,7 @@ func Test_shouldCreateArchive(t *testing.T) {
|
||||
extractedFiles := listFiles(extractionDir)
|
||||
|
||||
wasExtracted := func(p string) {
|
||||
fullpath := filesystem.JoinPaths(extractionDir, p)
|
||||
fullpath := path.Join(extractionDir, p)
|
||||
assert.Contains(t, extractedFiles, fullpath)
|
||||
copyContent, err := os.ReadFile(fullpath)
|
||||
require.NoError(t, err)
|
||||
@@ -74,25 +68,24 @@ func Test_shouldCreateArchive(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_shouldCreateArchive2(t *testing.T) {
|
||||
t.Parallel()
|
||||
tmpdir := t.TempDir()
|
||||
content := []byte("content")
|
||||
|
||||
err := os.WriteFile(filesystem.JoinPaths(tmpdir, "outer"), content, 0600)
|
||||
err := os.WriteFile(path.Join(tmpdir, "outer"), content, 0600)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = os.MkdirAll(filesystem.JoinPaths(tmpdir, "dir"), 0700)
|
||||
err = os.MkdirAll(path.Join(tmpdir, "dir"), 0700)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = os.WriteFile(filesystem.JoinPaths(tmpdir, "dir", ".dotfile"), content, 0600)
|
||||
err = os.WriteFile(path.Join(tmpdir, "dir", ".dotfile"), content, 0600)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = os.WriteFile(filesystem.JoinPaths(tmpdir, "dir", "inner"), content, 0600)
|
||||
err = os.WriteFile(path.Join(tmpdir, "dir", "inner"), content, 0600)
|
||||
require.NoError(t, err)
|
||||
|
||||
gzPath, err := TarGzDir(tmpdir)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, filesystem.JoinPaths(tmpdir, filepath.Base(tmpdir)+".tar.gz"), gzPath)
|
||||
assert.Equal(t, filepath.Join(tmpdir, filepath.Base(tmpdir)+".tar.gz"), gzPath)
|
||||
|
||||
extractionDir := t.TempDir()
|
||||
r, _ := os.Open(gzPath)
|
||||
@@ -102,7 +95,7 @@ func Test_shouldCreateArchive2(t *testing.T) {
|
||||
extractedFiles := listFiles(extractionDir)
|
||||
|
||||
wasExtracted := func(p string) {
|
||||
fullpath := filesystem.JoinPaths(extractionDir, p)
|
||||
fullpath := path.Join(extractionDir, p)
|
||||
assert.Contains(t, extractedFiles, fullpath)
|
||||
copyContent, _ := os.ReadFile(fullpath)
|
||||
assert.Equal(t, content, copyContent)
|
||||
@@ -112,57 +105,3 @@ func Test_shouldCreateArchive2(t *testing.T) {
|
||||
wasExtracted("dir/inner")
|
||||
wasExtracted("dir/.dotfile")
|
||||
}
|
||||
|
||||
func TestExtractTarGzPathTraversal(t *testing.T) {
|
||||
t.Parallel()
|
||||
testDir := t.TempDir()
|
||||
|
||||
// Create an evil file with a path traversal attempt
|
||||
tarPath := filesystem.JoinPaths(testDir, "evil.tar.gz")
|
||||
|
||||
evilFile, err := os.Create(tarPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
gzWriter := gzip.NewWriter(evilFile)
|
||||
tarWriter := tar.NewWriter(gzWriter)
|
||||
|
||||
content := []byte("evil content")
|
||||
|
||||
header := &tar.Header{
|
||||
Name: "../evil.txt",
|
||||
Mode: 0600,
|
||||
Size: int64(len(content)),
|
||||
Typeflag: tar.TypeReg,
|
||||
}
|
||||
|
||||
err = tarWriter.WriteHeader(header)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = tarWriter.Write(content)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = tarWriter.Close()
|
||||
require.NoError(t, err)
|
||||
|
||||
err = gzWriter.Close()
|
||||
require.NoError(t, err)
|
||||
|
||||
err = evilFile.Close()
|
||||
require.NoError(t, err)
|
||||
|
||||
// Attempt to extract the evil file
|
||||
extractionDir := filesystem.JoinPaths(testDir, "extraction")
|
||||
err = os.Mkdir(extractionDir, 0700)
|
||||
require.NoError(t, err)
|
||||
|
||||
tarFile, err := os.Open(tarPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Check that the file didn't escape
|
||||
err = ExtractTarGz(tarFile, extractionDir)
|
||||
require.NoError(t, err)
|
||||
require.NoFileExists(t, filesystem.JoinPaths(testDir, "evil.txt"))
|
||||
|
||||
err = tarFile.Close()
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
@@ -8,8 +8,6 @@ import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/portainer/portainer/api/logs"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
@@ -20,7 +18,7 @@ func UnzipFile(src string, dest string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer logs.CloseAndLogErr(r)
|
||||
defer r.Close()
|
||||
|
||||
for _, f := range r.File {
|
||||
p := filepath.Join(dest, f.Name)
|
||||
@@ -32,9 +30,7 @@ func UnzipFile(src string, dest string) error {
|
||||
|
||||
if f.FileInfo().IsDir() {
|
||||
// Make Folder
|
||||
if err := os.MkdirAll(p, os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
os.MkdirAll(p, os.ModePerm)
|
||||
|
||||
continue
|
||||
}
|
||||
@@ -57,13 +53,13 @@ func unzipFile(f *zip.File, p string) error {
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unzipFile: can't create file %s", p)
|
||||
}
|
||||
defer logs.CloseAndLogErr(outFile)
|
||||
defer outFile.Close()
|
||||
|
||||
rc, err := f.Open()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "unzipFile: can't open zip file %s in the archive", f.Name)
|
||||
}
|
||||
defer logs.CloseAndLogErr(rc)
|
||||
defer rc.Close()
|
||||
|
||||
if _, err = io.Copy(outFile, rc); err != nil {
|
||||
return errors.Wrapf(err, "unzipFile: can't copy an archived file content")
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
package archive
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/portainer/portainer/api/filesystem"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestUnzipFile(t *testing.T) {
|
||||
t.Parallel()
|
||||
dir := t.TempDir()
|
||||
/*
|
||||
Archive structure.
|
||||
@@ -25,8 +23,8 @@ func TestUnzipFile(t *testing.T) {
|
||||
|
||||
require.NoError(t, err)
|
||||
archiveDir := dir + "/sample_archive"
|
||||
assert.FileExists(t, filesystem.JoinPaths(archiveDir, "0.txt"))
|
||||
assert.FileExists(t, filesystem.JoinPaths(archiveDir, "0", "1.txt"))
|
||||
assert.FileExists(t, filesystem.JoinPaths(archiveDir, "0", "1", "2.txt"))
|
||||
assert.FileExists(t, filepath.Join(archiveDir, "0.txt"))
|
||||
assert.FileExists(t, filepath.Join(archiveDir, "0", "1.txt"))
|
||||
assert.FileExists(t, filepath.Join(archiveDir, "0", "1", "2.txt"))
|
||||
|
||||
}
|
||||
|
||||
@@ -8,8 +8,8 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s *Service) GetEncodedAuthorizationToken(ctx context.Context) (token *string, expiry *time.Time, err error) {
|
||||
getAuthorizationTokenOutput, err := s.client.GetAuthorizationToken(ctx, nil)
|
||||
func (s *Service) GetEncodedAuthorizationToken() (token *string, expiry *time.Time, err error) {
|
||||
getAuthorizationTokenOutput, err := s.client.GetAuthorizationToken(context.TODO(), nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -27,8 +27,8 @@ func (s *Service) GetEncodedAuthorizationToken(ctx context.Context) (token *stri
|
||||
return
|
||||
}
|
||||
|
||||
func (s *Service) GetAuthorizationToken(ctx context.Context) (token *string, expiry *time.Time, err error) {
|
||||
tokenEncodedStr, expiry, err := s.GetEncodedAuthorizationToken(ctx)
|
||||
func (s *Service) GetAuthorizationToken() (token *string, expiry *time.Time, err error) {
|
||||
tokenEncodedStr, expiry, err := s.GetEncodedAuthorizationToken()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -6,15 +6,6 @@ import (
|
||||
"github.com/aws/aws-sdk-go-v2/service/ecr"
|
||||
)
|
||||
|
||||
// Registry represents an ECR registry endpoint information.
|
||||
// This struct is used to parse and validate ECR endpoint URLs.
|
||||
type Registry struct {
|
||||
ID string // AWS account ID (empty for accountless endpoints like "ecr-fips.us-west-1.amazonaws.com")
|
||||
FIPS bool // Whether this is a FIPS endpoint (contains "-fips" in the URL)
|
||||
Region string // AWS region (e.g., "us-east-1", "us-gov-west-1")
|
||||
Public bool // Whether this is ecr-public.aws.com
|
||||
}
|
||||
|
||||
type (
|
||||
Service struct {
|
||||
accessKey string
|
||||
|
||||
@@ -1,70 +0,0 @@
|
||||
package ecr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ecrEndpointPattern matches all valid ECR endpoints including account-prefixed and accountless formats.
|
||||
// Based on AWS ECR credential helper regex but extended to support accountless endpoints.
|
||||
//
|
||||
// Supported formats:
|
||||
// - Account-prefixed: 123456789012.dkr.ecr-fips.us-east-1.amazonaws.com
|
||||
// - Account-prefixed (hyphen): 123456789012.dkr-ecr-fips.us-west-1.on.aws
|
||||
// - Accountless service: ecr-fips.us-west-1.amazonaws.com
|
||||
// - Accountless API: ecr-fips.us-east-1.api.aws
|
||||
// - Non-FIPS variants: All formats above without "-fips"
|
||||
//
|
||||
// Regex groups:
|
||||
// - Group 1: Full account prefix (optional) - e.g., "123456789012.dkr." or "123456789012.dkr-"
|
||||
// - Group 2: Account ID (optional) - e.g., "123456789012"
|
||||
// - Group 3: FIPS flag (optional) - either "-fips" or empty string
|
||||
// - Group 4: Region - e.g., "us-east-1", "us-gov-west-1"
|
||||
// - Group 5: Domain suffix - e.g., "amazonaws.com", "api.aws"
|
||||
var ecrEndpointPattern = regexp.MustCompile(
|
||||
`^((\d{12})\.dkr[\.\-])?ecr(\-fips)?\.([a-zA-Z0-9][a-zA-Z0-9-_]*)\.(amazonaws\.(?:com(?:\.cn)?|eu)|api\.aws|on\.(?:aws|amazonwebservices\.com\.cn)|sc2s\.sgov\.gov|c2s\.ic\.gov|cloud\.adc-e\.uk|csp\.hci\.ic\.gov)$`,
|
||||
)
|
||||
|
||||
// ParseECREndpoint parses an ECR registry URL and extracts registry information.
|
||||
|
||||
// This function replaces the AWS ECR credential helper library's ExtractRegistry function,
|
||||
// which only supports account-prefixed endpoints.
|
||||
//
|
||||
// Reference: https://docs.aws.amazon.com/general/latest/gr/ecr.html
|
||||
func ParseECREndpoint(urlStr string) (*Registry, error) {
|
||||
// Normalize URL by adding https:// prefix if not present
|
||||
if !strings.HasPrefix(urlStr, "https://") && !strings.HasPrefix(urlStr, "http://") {
|
||||
urlStr = "https://" + urlStr
|
||||
}
|
||||
|
||||
u, err := url.Parse(urlStr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid URL: %w", err)
|
||||
}
|
||||
|
||||
hostname := u.Hostname()
|
||||
|
||||
// Special case: ECR Public
|
||||
// ECR Public uses a different domain and doesn't have FIPS variant
|
||||
if hostname == "ecr-public.aws.com" {
|
||||
return &Registry{
|
||||
FIPS: false,
|
||||
Public: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Parse standard ECR endpoints using regex
|
||||
matches := ecrEndpointPattern.FindStringSubmatch(hostname)
|
||||
if len(matches) == 0 {
|
||||
return nil, fmt.Errorf("not a valid ECR endpoint: %s", hostname)
|
||||
}
|
||||
|
||||
return &Registry{
|
||||
ID: matches[2], // Account ID (may be empty for accountless endpoints)
|
||||
FIPS: matches[3] == "-fips", // Check if "-fips" is present
|
||||
Region: matches[4], // AWS region
|
||||
Public: false,
|
||||
}, nil
|
||||
}
|
||||
@@ -1,254 +0,0 @@
|
||||
package ecr
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseECREndpoint(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
name string
|
||||
url string
|
||||
want *Registry
|
||||
wantError bool
|
||||
}{
|
||||
// Standard AWS Commercial - Account-prefixed FIPS
|
||||
{
|
||||
name: "account-prefixed FIPS us-east-1",
|
||||
url: "123456789012.dkr.ecr-fips.us-east-1.amazonaws.com",
|
||||
want: &Registry{
|
||||
ID: "123456789012",
|
||||
FIPS: true,
|
||||
Region: "us-east-1",
|
||||
Public: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "account-prefixed FIPS us-west-2",
|
||||
url: "123456789012.dkr.ecr-fips.us-west-2.amazonaws.com",
|
||||
want: &Registry{
|
||||
ID: "123456789012",
|
||||
FIPS: true,
|
||||
Region: "us-west-2",
|
||||
Public: false,
|
||||
},
|
||||
},
|
||||
|
||||
// Accountless FIPS service endpoints
|
||||
{
|
||||
name: "accountless FIPS us-west-1",
|
||||
url: "ecr-fips.us-west-1.amazonaws.com",
|
||||
want: &Registry{
|
||||
ID: "",
|
||||
FIPS: true,
|
||||
Region: "us-west-1",
|
||||
Public: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accountless FIPS us-east-2",
|
||||
url: "ecr-fips.us-east-2.amazonaws.com",
|
||||
want: &Registry{
|
||||
ID: "",
|
||||
FIPS: true,
|
||||
Region: "us-east-2",
|
||||
Public: false,
|
||||
},
|
||||
},
|
||||
|
||||
// Accountless FIPS API endpoints
|
||||
{
|
||||
name: "accountless FIPS API us-west-1",
|
||||
url: "ecr-fips.us-west-1.api.aws",
|
||||
want: &Registry{
|
||||
ID: "",
|
||||
FIPS: true,
|
||||
Region: "us-west-1",
|
||||
Public: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accountless FIPS API us-east-1",
|
||||
url: "ecr-fips.us-east-1.api.aws",
|
||||
want: &Registry{
|
||||
ID: "",
|
||||
FIPS: true,
|
||||
Region: "us-east-1",
|
||||
Public: false,
|
||||
},
|
||||
},
|
||||
|
||||
// on.aws domain with hyphen separator
|
||||
{
|
||||
name: "account-prefixed FIPS hyphen us-west-1",
|
||||
url: "123456789012.dkr-ecr-fips.us-west-1.on.aws",
|
||||
want: &Registry{
|
||||
ID: "123456789012",
|
||||
FIPS: true,
|
||||
Region: "us-west-1",
|
||||
Public: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "account-prefixed FIPS hyphen us-east-2",
|
||||
url: "123456789012.dkr-ecr-fips.us-east-2.on.aws",
|
||||
want: &Registry{
|
||||
ID: "123456789012",
|
||||
FIPS: true,
|
||||
Region: "us-east-2",
|
||||
Public: false,
|
||||
},
|
||||
},
|
||||
|
||||
// AWS GovCloud
|
||||
{
|
||||
name: "account-prefixed FIPS us-gov-east-1",
|
||||
url: "123456789012.dkr.ecr-fips.us-gov-east-1.amazonaws.com",
|
||||
want: &Registry{
|
||||
ID: "123456789012",
|
||||
FIPS: true,
|
||||
Region: "us-gov-east-1",
|
||||
Public: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "account-prefixed FIPS us-gov-west-1",
|
||||
url: "123456789012.dkr.ecr-fips.us-gov-west-1.amazonaws.com",
|
||||
want: &Registry{
|
||||
ID: "123456789012",
|
||||
FIPS: true,
|
||||
Region: "us-gov-west-1",
|
||||
Public: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accountless FIPS us-gov-west-1",
|
||||
url: "ecr-fips.us-gov-west-1.amazonaws.com",
|
||||
want: &Registry{
|
||||
ID: "",
|
||||
FIPS: true,
|
||||
Region: "us-gov-west-1",
|
||||
Public: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accountless FIPS API us-gov-east-1",
|
||||
url: "ecr-fips.us-gov-east-1.api.aws",
|
||||
want: &Registry{
|
||||
ID: "",
|
||||
FIPS: true,
|
||||
Region: "us-gov-east-1",
|
||||
Public: false,
|
||||
},
|
||||
},
|
||||
|
||||
// ECR Public
|
||||
{
|
||||
name: "ecr-public",
|
||||
url: "ecr-public.aws.com",
|
||||
want: &Registry{
|
||||
ID: "",
|
||||
FIPS: false,
|
||||
Region: "",
|
||||
Public: true,
|
||||
},
|
||||
},
|
||||
|
||||
// Non-FIPS endpoints (valid ECR but FIPS=false)
|
||||
{
|
||||
name: "account-prefixed non-FIPS us-east-1",
|
||||
url: "123456789012.dkr.ecr.us-east-1.amazonaws.com",
|
||||
want: &Registry{
|
||||
ID: "123456789012",
|
||||
FIPS: false,
|
||||
Region: "us-east-1",
|
||||
Public: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accountless non-FIPS us-west-1",
|
||||
url: "ecr.us-west-1.amazonaws.com",
|
||||
want: &Registry{
|
||||
ID: "",
|
||||
FIPS: false,
|
||||
Region: "us-west-1",
|
||||
Public: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "accountless non-FIPS API us-east-2",
|
||||
url: "ecr.us-east-2.api.aws",
|
||||
want: &Registry{
|
||||
ID: "",
|
||||
FIPS: false,
|
||||
Region: "us-east-2",
|
||||
Public: false,
|
||||
},
|
||||
},
|
||||
|
||||
// URLs with https:// prefix
|
||||
{
|
||||
name: "with https prefix",
|
||||
url: "https://ecr-fips.us-west-1.amazonaws.com",
|
||||
want: &Registry{
|
||||
ID: "",
|
||||
FIPS: true,
|
||||
Region: "us-west-1",
|
||||
Public: false,
|
||||
},
|
||||
},
|
||||
|
||||
// Invalid endpoints
|
||||
{
|
||||
name: "not an ECR URL",
|
||||
url: "not-an-ecr-url.com",
|
||||
wantError: true,
|
||||
},
|
||||
{
|
||||
name: "invalid account ID length",
|
||||
url: "123.dkr.ecr-fips.us-east-1.amazonaws.com",
|
||||
wantError: true,
|
||||
},
|
||||
{
|
||||
name: "empty string",
|
||||
url: "",
|
||||
wantError: true,
|
||||
},
|
||||
{
|
||||
name: "docker hub",
|
||||
url: "docker.io",
|
||||
wantError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := ParseECREndpoint(tt.url)
|
||||
|
||||
if tt.wantError {
|
||||
if err == nil {
|
||||
t.Errorf("ParseECREndpoint() expected error but got none")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("ParseECREndpoint() unexpected error: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if got.ID != tt.want.ID {
|
||||
t.Errorf("ParseECREndpoint() ID = %v, want %v", got.ID, tt.want.ID)
|
||||
}
|
||||
if got.FIPS != tt.want.FIPS {
|
||||
t.Errorf("ParseECREndpoint() FIPS = %v, want %v", got.FIPS, tt.want.FIPS)
|
||||
}
|
||||
if got.Region != tt.want.Region {
|
||||
t.Errorf("ParseECREndpoint() Region = %v, want %v", got.Region, tt.want.Region)
|
||||
}
|
||||
if got.Public != tt.want.Public {
|
||||
t.Errorf("ParseECREndpoint() Public = %v, want %v", got.Public, tt.want.Public)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"github.com/portainer/portainer/api/dataservices"
|
||||
"github.com/portainer/portainer/api/filesystem"
|
||||
"github.com/portainer/portainer/api/http/offlinegate"
|
||||
"github.com/portainer/portainer/api/logs"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/rs/zerolog/log"
|
||||
@@ -98,7 +97,7 @@ func encrypt(path string, passphrase string) (string, error) {
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer logs.CloseAndLogErr(in)
|
||||
defer in.Close()
|
||||
|
||||
outFileName := path + ".encrypted"
|
||||
out, err := os.Create(outFileName)
|
||||
@@ -106,5 +105,7 @@ func encrypt(path string, passphrase string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return outFileName, crypto.AesEncrypt(in, out, []byte(passphrase))
|
||||
err = crypto.AesEncrypt(in, out, []byte(passphrase))
|
||||
|
||||
return outFileName, err
|
||||
}
|
||||
|
||||
@@ -1,274 +0,0 @@
|
||||
package backup
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/portainer/portainer/api/archive"
|
||||
"github.com/portainer/portainer/api/crypto"
|
||||
"github.com/portainer/portainer/api/datastore"
|
||||
"github.com/portainer/portainer/api/filesystem"
|
||||
"github.com/portainer/portainer/api/http/offlinegate"
|
||||
"github.com/portainer/portainer/pkg/fips"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func init() {
|
||||
fips.InitFIPS(false)
|
||||
}
|
||||
|
||||
func TestGetRestoreSourcePath_DBAtRoot(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir := t.TempDir()
|
||||
err := os.WriteFile(filesystem.JoinPaths(dir, "portainer.db"), []byte("db"), 0o600)
|
||||
require.NoError(t, err)
|
||||
|
||||
result, err := getRestoreSourcePath(dir)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, dir, result)
|
||||
}
|
||||
|
||||
func TestGetRestoreSourcePath_EncryptedDBAtRoot(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir := t.TempDir()
|
||||
err := os.WriteFile(filesystem.JoinPaths(dir, "portainer.edb"), []byte("db"), 0o600)
|
||||
require.NoError(t, err)
|
||||
|
||||
result, err := getRestoreSourcePath(dir)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, dir, result)
|
||||
}
|
||||
|
||||
func TestGetRestoreSourcePath_DBInSubdirectory(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir := t.TempDir()
|
||||
sub := filesystem.JoinPaths(dir, "backup-2024-01-01")
|
||||
err := os.Mkdir(sub, 0o700)
|
||||
require.NoError(t, err)
|
||||
|
||||
err = os.WriteFile(filesystem.JoinPaths(sub, "portainer.db"), []byte("db"), 0o600)
|
||||
require.NoError(t, err)
|
||||
|
||||
result, err := getRestoreSourcePath(dir)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, sub, result)
|
||||
}
|
||||
|
||||
func TestGetRestoreSourcePath_NoDBFile(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir := t.TempDir()
|
||||
err := os.WriteFile(filesystem.JoinPaths(dir, "other.file"), []byte("data"), 0o600)
|
||||
require.NoError(t, err)
|
||||
|
||||
result, err := getRestoreSourcePath(dir)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, dir, result)
|
||||
}
|
||||
|
||||
func TestGetRestoreSourcePath_EmptyDir(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir := t.TempDir()
|
||||
|
||||
result, err := getRestoreSourcePath(dir)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, dir, result)
|
||||
}
|
||||
|
||||
func TestEncryptDecrypt_RoundTrip(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir := t.TempDir()
|
||||
plaintext := []byte("sensitive portainer backup data")
|
||||
|
||||
srcPath := filesystem.JoinPaths(dir, "archive.tar.gz")
|
||||
err := os.WriteFile(srcPath, plaintext, 0o600)
|
||||
require.NoError(t, err)
|
||||
|
||||
encryptedPath, err := encrypt(srcPath, "mysecretpassword")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, srcPath+".encrypted", encryptedPath)
|
||||
|
||||
encryptedData, err := os.ReadFile(encryptedPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
decryptedReader, err := crypto.AesDecrypt(bytes.NewReader(encryptedData), []byte("mysecretpassword"))
|
||||
require.NoError(t, err)
|
||||
|
||||
decrypted, err := io.ReadAll(decryptedReader)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, plaintext, decrypted)
|
||||
}
|
||||
|
||||
func TestEncryptDecrypt_WrongPassword(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
dir := t.TempDir()
|
||||
|
||||
srcPath := filesystem.JoinPaths(dir, "archive.tar.gz")
|
||||
err := os.WriteFile(srcPath, []byte("data"), 0o600)
|
||||
require.NoError(t, err)
|
||||
|
||||
encryptedPath, err := encrypt(srcPath, "correctpassword")
|
||||
require.NoError(t, err)
|
||||
|
||||
encryptedData, err := os.ReadFile(encryptedPath)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = crypto.AesDecrypt(bytes.NewReader(encryptedData), []byte("wrongpassword"))
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestCreateBackupArchive_NoPassword(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, store := datastore.MustNewTestStore(t, true, false)
|
||||
storePath := store.GetConnection().GetStorePath()
|
||||
gate := offlinegate.NewOfflineGate()
|
||||
|
||||
archivePath, err := CreateBackupArchive("", gate, store, storePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
f, err := os.Open(archivePath)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
err := f.Close()
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
extractDir := t.TempDir()
|
||||
err = archive.ExtractTarGz(f, extractDir)
|
||||
require.NoError(t, err)
|
||||
|
||||
dbFound := false
|
||||
err = filepath.Walk(extractDir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.Name() == "portainer.db" {
|
||||
dbFound = true
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, dbFound, "archive should contain portainer.db")
|
||||
}
|
||||
|
||||
func TestCreateBackupArchive_WithPassword(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, store := datastore.MustNewTestStore(t, true, false)
|
||||
storePath := store.GetConnection().GetStorePath()
|
||||
gate := offlinegate.NewOfflineGate()
|
||||
|
||||
archivePath, err := CreateBackupArchive("backup-secret", gate, store, storePath)
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, archivePath, ".encrypted")
|
||||
|
||||
encryptedData, err := os.ReadFile(archivePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
decryptedReader, err := crypto.AesDecrypt(bytes.NewReader(encryptedData), []byte("backup-secret"))
|
||||
require.NoError(t, err)
|
||||
|
||||
extractDir := t.TempDir()
|
||||
err = archive.ExtractTarGz(decryptedReader, extractDir)
|
||||
require.NoError(t, err)
|
||||
|
||||
dbFound := false
|
||||
err = filepath.Walk(extractDir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.Name() == "portainer.db" {
|
||||
dbFound = true
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.True(t, dbFound, "decrypted archive should contain portainer.db")
|
||||
}
|
||||
|
||||
func TestRestoreArchive_NoPassword(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, store1 := datastore.MustNewTestStore(t, true, false)
|
||||
storePath1 := store1.GetConnection().GetStorePath()
|
||||
gate := offlinegate.NewOfflineGate()
|
||||
|
||||
archivePath, err := CreateBackupArchive("", gate, store1, storePath1)
|
||||
require.NoError(t, err)
|
||||
|
||||
archiveData, err := os.ReadFile(archivePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, store2 := datastore.MustNewTestStore(t, true, false)
|
||||
storePath2 := store2.GetConnection().GetStorePath()
|
||||
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
err = RestoreArchive(bytes.NewReader(archiveData), "", storePath2, gate, store2, cancel)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.ErrorIs(t, ctx.Err(), context.Canceled)
|
||||
|
||||
_, err = os.Stat(filesystem.JoinPaths(storePath2, "portainer.db"))
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestRestoreArchive_WithPassword(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, store1 := datastore.MustNewTestStore(t, true, false)
|
||||
storePath1 := store1.GetConnection().GetStorePath()
|
||||
gate := offlinegate.NewOfflineGate()
|
||||
|
||||
archivePath, err := CreateBackupArchive("restore-secret", gate, store1, storePath1)
|
||||
require.NoError(t, err)
|
||||
|
||||
archiveData, err := os.ReadFile(archivePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, store2 := datastore.MustNewTestStore(t, true, false)
|
||||
storePath2 := store2.GetConnection().GetStorePath()
|
||||
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
err = RestoreArchive(bytes.NewReader(archiveData), "restore-secret", storePath2, gate, store2, cancel)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.ErrorIs(t, ctx.Err(), context.Canceled)
|
||||
|
||||
_, err = os.Stat(filesystem.JoinPaths(storePath2, "portainer.db"))
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestRestoreArchive_WrongPassword(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, store1 := datastore.MustNewTestStore(t, true, false)
|
||||
storePath1 := store1.GetConnection().GetStorePath()
|
||||
gate := offlinegate.NewOfflineGate()
|
||||
|
||||
archivePath, err := CreateBackupArchive("correct-password", gate, store1, storePath1)
|
||||
require.NoError(t, err)
|
||||
|
||||
archiveData, err := os.ReadFile(archivePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
_, store2 := datastore.MustNewTestStore(t, true, false)
|
||||
storePath2 := store2.GetConnection().GetStorePath()
|
||||
|
||||
_, cancel := context.WithCancel(t.Context())
|
||||
err = RestoreArchive(bytes.NewReader(archiveData), "wrong-password", storePath2, gate, store2, cancel)
|
||||
require.Error(t, err)
|
||||
}
|
||||
@@ -16,8 +16,6 @@ import (
|
||||
"github.com/portainer/portainer/api/dataservices"
|
||||
"github.com/portainer/portainer/api/filesystem"
|
||||
"github.com/portainer/portainer/api/http/offlinegate"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
var filesToRestore = append(filesToBackup, "portainer.db")
|
||||
@@ -33,20 +31,17 @@ func RestoreArchive(archive io.Reader, password string, filestorePath string, ga
|
||||
}
|
||||
|
||||
restorePath := filepath.Join(filestorePath, "restore", time.Now().Format("20060102150405"))
|
||||
defer func() {
|
||||
if err := os.RemoveAll(filepath.Dir(restorePath)); err != nil {
|
||||
log.Warn().Err(err).Msg("failed to clean up restore files")
|
||||
}
|
||||
}()
|
||||
defer os.RemoveAll(filepath.Dir(restorePath))
|
||||
|
||||
if err := extractArchive(archive, restorePath); err != nil {
|
||||
err = extractArchive(archive, restorePath)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "cannot extract files from the archive. Please ensure the password is correct and try again")
|
||||
}
|
||||
|
||||
unlock := gate.Lock()
|
||||
defer unlock()
|
||||
|
||||
if err := datastore.Close(); err != nil {
|
||||
if err = datastore.Close(); err != nil {
|
||||
return errors.Wrap(err, "Failed to stop db")
|
||||
}
|
||||
|
||||
@@ -56,7 +51,7 @@ func RestoreArchive(archive io.Reader, password string, filestorePath string, ga
|
||||
return errors.Wrap(err, "failed to restore from backup. Portainer database missing from backup file")
|
||||
}
|
||||
|
||||
if err := restoreFiles(restorePath, filestorePath); err != nil {
|
||||
if err = restoreFiles(restorePath, filestorePath); err != nil {
|
||||
return errors.Wrap(err, "failed to restore the system state")
|
||||
}
|
||||
|
||||
@@ -94,7 +89,8 @@ func getRestoreSourcePath(dir string) (string, error) {
|
||||
|
||||
func restoreFiles(srcDir string, destinationDir string) error {
|
||||
for _, filename := range filesToRestore {
|
||||
if err := filesystem.CopyPath(filepath.Join(srcDir, filename), destinationDir); err != nil {
|
||||
err := filesystem.CopyPath(filepath.Join(srcDir, filename), destinationDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -102,18 +98,14 @@ func restoreFiles(srcDir string, destinationDir string) error {
|
||||
// TODO: This is very boltdb module specific once again due to the filename. Move to bolt module? Refactor for another day
|
||||
|
||||
// Prevent the possibility of having both databases. Remove any default new instance
|
||||
if err := os.Remove(filepath.Join(destinationDir, boltdb.DatabaseFileName)); err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.Remove(filepath.Join(destinationDir, boltdb.EncryptedDatabaseFileName)); err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
os.Remove(filepath.Join(destinationDir, boltdb.DatabaseFileName))
|
||||
os.Remove(filepath.Join(destinationDir, boltdb.EncryptedDatabaseFileName))
|
||||
|
||||
// Now copy the database. It'll be either portainer.db or portainer.edb
|
||||
|
||||
// Note: CopyPath does not return an error if the source file doesn't exist
|
||||
if err := filesystem.CopyPath(filepath.Join(srcDir, boltdb.EncryptedDatabaseFileName), destinationDir); err != nil {
|
||||
err := filesystem.CopyPath(filepath.Join(srcDir, boltdb.EncryptedDatabaseFileName), destinationDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
)
|
||||
|
||||
func TestGenerateGo119CompatibleKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
type args struct {
|
||||
seed string
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/dataservices"
|
||||
"github.com/portainer/portainer/api/http/proxy"
|
||||
"github.com/portainer/portainer/pkg/schedule"
|
||||
|
||||
chserver "github.com/jpillora/chisel/server"
|
||||
"github.com/jpillora/chisel/share/ccrypto"
|
||||
@@ -90,8 +89,10 @@ func (service *Service) pingAgent(endpointID portainer.EndpointID) error {
|
||||
return err
|
||||
}
|
||||
|
||||
_, _ = io.Copy(io.Discard, resp.Body)
|
||||
return resp.Body.Close()
|
||||
io.Copy(io.Discard, resp.Body)
|
||||
resp.Body.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// KeepTunnelAlive keeps the tunnel of the given environment for maxAlive duration, or until ctx is done
|
||||
@@ -234,18 +235,27 @@ func (service *Service) startTunnelVerificationLoop() {
|
||||
Float64("check_interval_seconds", tunnelCleanupInterval.Seconds()).
|
||||
Msg("starting tunnel management process")
|
||||
|
||||
schedule.RunOnInterval(service.shutdownCtx, tunnelCleanupInterval, service.checkTunnels, func() {
|
||||
log.Debug().Msg("shutting down tunnel service")
|
||||
ticker := time.NewTicker(tunnelCleanupInterval)
|
||||
|
||||
if err := service.StopTunnelServer(); err != nil {
|
||||
log.Debug().Err(err).Msg("stopped tunnel service")
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
service.checkTunnels()
|
||||
case <-service.shutdownCtx.Done():
|
||||
log.Debug().Msg("shutting down tunnel service")
|
||||
|
||||
if err := service.StopTunnelServer(); err != nil {
|
||||
log.Debug().Err(err).Msg("stopped tunnel service")
|
||||
}
|
||||
|
||||
ticker.Stop()
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// checkTunnels finds tunnels that need snapshots and processes them one at a time.
|
||||
// For active tunnels missing an initial snapshot, it takes one without closing the tunnel.
|
||||
// For tunnels idle past activeTimeout, it snapshots and closes them.
|
||||
// checkTunnels finds the first tunnel that has not had any activity recently
|
||||
// and attempts to take a snapshot, then closes it and returns
|
||||
func (service *Service) checkTunnels() {
|
||||
service.mu.RLock()
|
||||
|
||||
@@ -256,32 +266,12 @@ func (service *Service) checkTunnels() {
|
||||
Float64("last_activity_seconds", elapsed.Seconds()).
|
||||
Msg("environment tunnel monitoring")
|
||||
|
||||
tunnelPort := tunnel.Port
|
||||
|
||||
if !tunnel.HasSnapshot && elapsed < activeTimeout {
|
||||
service.mu.RUnlock()
|
||||
|
||||
if endpointHasSnapshot(service.dataStore, endpointID) {
|
||||
service.markSnapshotTaken(endpointID)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
log.Debug().
|
||||
Int("endpoint_id", int(endpointID)).
|
||||
Msg("taking initial snapshot for active Edge environment")
|
||||
|
||||
if service.snapshotAndLog(endpointID, tunnelPort) {
|
||||
service.markSnapshotTaken(endpointID)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if tunnel.Status == portainer.EdgeAgentManagementRequired && elapsed < activeTimeout {
|
||||
continue
|
||||
}
|
||||
|
||||
tunnelPort := tunnel.Port
|
||||
|
||||
service.mu.RUnlock()
|
||||
|
||||
log.Debug().
|
||||
@@ -290,7 +280,13 @@ func (service *Service) checkTunnels() {
|
||||
Float64("timeout_seconds", activeTimeout.Seconds()).
|
||||
Msg("last activity timeout exceeded")
|
||||
|
||||
service.snapshotAndLog(endpointID, tunnelPort)
|
||||
if err := service.snapshotEnvironment(endpointID, tunnelPort); err != nil {
|
||||
log.Error().
|
||||
Int("endpoint_id", int(endpointID)).
|
||||
Err(err).
|
||||
Msg("unable to snapshot Edge environment")
|
||||
}
|
||||
|
||||
service.close(endpointID)
|
||||
|
||||
return
|
||||
@@ -299,32 +295,6 @@ func (service *Service) checkTunnels() {
|
||||
service.mu.RUnlock()
|
||||
}
|
||||
|
||||
func (service *Service) snapshotAndLog(endpointID portainer.EndpointID, tunnelPort int) bool {
|
||||
if err := service.snapshotEnvironment(endpointID, tunnelPort); err != nil {
|
||||
log.Error().
|
||||
Int("endpoint_id", int(endpointID)).
|
||||
Err(err).
|
||||
Msg("unable to snapshot Edge environment")
|
||||
|
||||
if service.dataStore.IsErrObjectNotFound(err) {
|
||||
service.close(endpointID)
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (service *Service) markSnapshotTaken(endpointID portainer.EndpointID) {
|
||||
service.mu.Lock()
|
||||
defer service.mu.Unlock()
|
||||
|
||||
if tun, ok := service.activeTunnels[endpointID]; ok {
|
||||
tun.HasSnapshot = true
|
||||
}
|
||||
}
|
||||
|
||||
func (service *Service) snapshotEnvironment(endpointID portainer.EndpointID, tunnelPort int) error {
|
||||
endpoint, err := service.dataStore.Endpoint().Endpoint(endpointID)
|
||||
if err != nil {
|
||||
|
||||
@@ -2,7 +2,6 @@ package chisel
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"testing"
|
||||
@@ -19,38 +18,15 @@ func init() {
|
||||
fips.InitFIPS(false)
|
||||
}
|
||||
|
||||
type mockSnapshotService struct {
|
||||
snapshotFn func(endpoint *portainer.Endpoint) error
|
||||
}
|
||||
|
||||
func (m *mockSnapshotService) Start(_ context.Context) {}
|
||||
|
||||
func (m *mockSnapshotService) SetSnapshotInterval(_ string) error { return nil }
|
||||
|
||||
func (m *mockSnapshotService) SnapshotEndpoint(endpoint *portainer.Endpoint) error {
|
||||
if m.snapshotFn != nil {
|
||||
return m.snapshotFn(endpoint)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *mockSnapshotService) FillSnapshotData(_ *portainer.Endpoint, _ bool) error { return nil }
|
||||
|
||||
func newEdgeEndpoint(id portainer.EndpointID) *portainer.Endpoint {
|
||||
return &portainer.Endpoint{
|
||||
ID: id,
|
||||
func TestPingAgentPanic(t *testing.T) {
|
||||
endpoint := &portainer.Endpoint{
|
||||
ID: 1,
|
||||
EdgeID: "test-edge-id",
|
||||
Type: portainer.EdgeAgentOnDockerEnvironment,
|
||||
UserTrusted: true,
|
||||
}
|
||||
}
|
||||
|
||||
func TestPingAgentPanic(t *testing.T) {
|
||||
t.Parallel()
|
||||
endpoint := newEdgeEndpoint(1)
|
||||
|
||||
_, store := datastore.MustNewTestStore(t, false, true)
|
||||
_, store := datastore.MustNewTestStore(t, true, true)
|
||||
|
||||
s := NewService(store, nil, nil)
|
||||
|
||||
@@ -78,161 +54,6 @@ func TestPingAgentPanic(t *testing.T) {
|
||||
s.activeTunnels[endpoint.ID].Port = ln.Addr().(*net.TCPAddr).Port
|
||||
|
||||
require.Error(t, s.pingAgent(endpoint.ID))
|
||||
require.NoError(t, srv.Shutdown(t.Context()))
|
||||
require.NoError(t, srv.Shutdown(context.Background()))
|
||||
require.ErrorIs(t, <-errCh, http.ErrServerClosed)
|
||||
}
|
||||
|
||||
func TestOpenDefaultsHasSnapshotToFalse(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
endpoint := newEdgeEndpoint(1)
|
||||
_, store := datastore.MustNewTestStore(t, false, true)
|
||||
|
||||
s := NewService(store, nil, nil)
|
||||
|
||||
err := s.Open(endpoint)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.False(t, s.activeTunnels[endpoint.ID].HasSnapshot)
|
||||
}
|
||||
|
||||
func TestCheckTunnelsSetsHasSnapshotWhenSnapshotExists(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
endpoint := newEdgeEndpoint(2)
|
||||
_, store := datastore.MustNewTestStore(t, false, true)
|
||||
|
||||
err := store.Endpoint().Create(endpoint)
|
||||
require.NoError(t, err)
|
||||
|
||||
snap := &portainer.Snapshot{
|
||||
EndpointID: endpoint.ID,
|
||||
Docker: &portainer.DockerSnapshot{},
|
||||
}
|
||||
err = store.Snapshot().Create(snap)
|
||||
require.NoError(t, err)
|
||||
|
||||
s := NewService(store, nil, nil)
|
||||
s.activeTunnels[endpoint.ID] = &portainer.TunnelDetails{
|
||||
Status: portainer.EdgeAgentManagementRequired,
|
||||
Port: 50003,
|
||||
LastActivity: time.Now(),
|
||||
}
|
||||
|
||||
s.checkTunnels()
|
||||
|
||||
require.NotNil(t, s.activeTunnels[endpoint.ID], "tunnel must remain open")
|
||||
require.True(t, s.activeTunnels[endpoint.ID].HasSnapshot)
|
||||
}
|
||||
|
||||
func TestCheckTunnelsSnapshotsActiveEnvironmentAndKeepsTunnelAlive(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
endpoint := newEdgeEndpoint(3)
|
||||
_, store := datastore.MustNewTestStore(t, false, true)
|
||||
|
||||
err := store.Endpoint().Create(endpoint)
|
||||
require.NoError(t, err)
|
||||
|
||||
snapshotCalled := false
|
||||
svc := &mockSnapshotService{
|
||||
snapshotFn: func(_ *portainer.Endpoint) error {
|
||||
snapshotCalled = true
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
s := NewService(store, nil, nil)
|
||||
s.snapshotService = svc
|
||||
s.activeTunnels[endpoint.ID] = &portainer.TunnelDetails{
|
||||
Status: portainer.EdgeAgentManagementRequired,
|
||||
Port: 50000,
|
||||
LastActivity: time.Now(),
|
||||
}
|
||||
|
||||
s.checkTunnels()
|
||||
|
||||
require.True(t, snapshotCalled)
|
||||
require.NotNil(t, s.activeTunnels[endpoint.ID], "tunnel must remain open after snapshot")
|
||||
require.True(t, s.activeTunnels[endpoint.ID].HasSnapshot)
|
||||
}
|
||||
|
||||
func TestCheckTunnelsKeepsHasSnapshotFalseOnSnapshotFailure(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
endpoint := newEdgeEndpoint(4)
|
||||
_, store := datastore.MustNewTestStore(t, false, true)
|
||||
|
||||
err := store.Endpoint().Create(endpoint)
|
||||
require.NoError(t, err)
|
||||
|
||||
svc := &mockSnapshotService{
|
||||
snapshotFn: func(_ *portainer.Endpoint) error {
|
||||
return errors.New("snapshot failed")
|
||||
},
|
||||
}
|
||||
|
||||
s := NewService(store, nil, nil)
|
||||
s.snapshotService = svc
|
||||
s.activeTunnels[endpoint.ID] = &portainer.TunnelDetails{
|
||||
Status: portainer.EdgeAgentManagementRequired,
|
||||
Port: 50001,
|
||||
LastActivity: time.Now(),
|
||||
}
|
||||
|
||||
s.checkTunnels()
|
||||
|
||||
require.NotNil(t, s.activeTunnels[endpoint.ID], "tunnel must remain open after failed snapshot")
|
||||
require.False(t, s.activeTunnels[endpoint.ID].HasSnapshot, "HasSnapshot must stay false after failure")
|
||||
}
|
||||
|
||||
func TestCheckTunnelsClosesStaleEntryForDeletedEndpoint(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
_, store := datastore.MustNewTestStore(t, false, true)
|
||||
|
||||
// Endpoint is not created in the store, simulates deletion while tunnel stays open.
|
||||
s := NewService(store, nil, nil)
|
||||
s.activeTunnels[1] = &portainer.TunnelDetails{
|
||||
Status: portainer.EdgeAgentManagementRequired,
|
||||
Port: 50010,
|
||||
LastActivity: time.Now(),
|
||||
}
|
||||
|
||||
s.checkTunnels()
|
||||
|
||||
require.Nil(t, s.activeTunnels[1], "stale tunnel for deleted endpoint must be removed immediately")
|
||||
}
|
||||
|
||||
func TestCheckTunnelsClosesIdleTunnelAndSnapshots(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
endpoint := newEdgeEndpoint(5)
|
||||
_, store := datastore.MustNewTestStore(t, false, true)
|
||||
|
||||
err := store.Endpoint().Create(endpoint)
|
||||
require.NoError(t, err)
|
||||
|
||||
snapshotCalled := false
|
||||
svc := &mockSnapshotService{
|
||||
snapshotFn: func(_ *portainer.Endpoint) error {
|
||||
snapshotCalled = true
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
s := NewService(store, nil, nil)
|
||||
s.snapshotService = svc
|
||||
s.activeTunnels[endpoint.ID] = &portainer.TunnelDetails{
|
||||
Status: portainer.EdgeAgentManagementRequired,
|
||||
Port: 50002,
|
||||
LastActivity: time.Now().Add(-(activeTimeout + time.Second)),
|
||||
}
|
||||
|
||||
s.checkTunnels()
|
||||
|
||||
require.True(t, snapshotCalled)
|
||||
require.Nil(t, s.activeTunnels[endpoint.ID], "tunnel must be closed after idle timeout")
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"time"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/dataservices"
|
||||
"github.com/portainer/portainer/api/internal/edge"
|
||||
"github.com/portainer/portainer/api/internal/edge/cache"
|
||||
"github.com/portainer/portainer/api/internal/endpointutils"
|
||||
@@ -82,24 +81,17 @@ func (s *Service) Open(endpoint *portainer.Endpoint) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// close removes the tunnel from the map so the agent will close it.
|
||||
// The lock is released before cleaning up the chisel user and proxy to avoid
|
||||
// blocking Config/Open callers while DeleteUser interacts with chisel internals.
|
||||
// close removes the tunnel from the map so the agent will close it
|
||||
func (s *Service) close(endpointID portainer.EndpointID) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
tun, ok := s.activeTunnels[endpointID]
|
||||
if !ok {
|
||||
s.mu.Unlock()
|
||||
return
|
||||
}
|
||||
|
||||
delete(s.activeTunnels, endpointID)
|
||||
cache.Del(endpointID)
|
||||
|
||||
s.mu.Unlock()
|
||||
|
||||
if s.chiselServer != nil {
|
||||
if len(tun.Credentials) > 0 && s.chiselServer != nil {
|
||||
user, _, _ := strings.Cut(tun.Credentials, ":")
|
||||
s.chiselServer.DeleteUser(user)
|
||||
}
|
||||
@@ -107,6 +99,10 @@ func (s *Service) close(endpointID portainer.EndpointID) {
|
||||
if s.ProxyManager != nil {
|
||||
s.ProxyManager.DeleteEndpointProxy(endpointID)
|
||||
}
|
||||
|
||||
delete(s.activeTunnels, endpointID)
|
||||
|
||||
cache.Del(endpointID)
|
||||
}
|
||||
|
||||
// Config returns the tunnel details needed for the agent to connect
|
||||
@@ -146,9 +142,7 @@ func (s *Service) TunnelAddr(endpoint *portainer.Endpoint) (string, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
if err := conn.Close(); err != nil {
|
||||
log.Warn().Err(err).Msg("failed to close tcp connection")
|
||||
}
|
||||
conn.Close()
|
||||
|
||||
break
|
||||
}
|
||||
@@ -241,18 +235,3 @@ func encryptCredentials(username, password, key string) (string, error) {
|
||||
|
||||
return base64.RawStdEncoding.EncodeToString(encryptedCredentials), nil
|
||||
}
|
||||
|
||||
func endpointHasSnapshot(dataStore dataservices.DataStore, endpointID portainer.EndpointID) bool {
|
||||
var hasSnapshot bool
|
||||
_ = dataStore.ViewTx(func(tx dataservices.DataStoreTx) error {
|
||||
s, err := tx.Snapshot().Read(endpointID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hasSnapshot = s.Docker != nil || s.Kubernetes != nil
|
||||
return nil
|
||||
})
|
||||
|
||||
return hasSnapshot
|
||||
}
|
||||
|
||||
@@ -28,7 +28,6 @@ func (s *testStore) Settings() dataservices.SettingsService {
|
||||
}
|
||||
|
||||
func TestGetUnusedPort(t *testing.T) {
|
||||
t.Parallel()
|
||||
testCases := []struct {
|
||||
name string
|
||||
existingTunnels map[portainer.EndpointID]*portainer.TunnelDetails
|
||||
|
||||
@@ -32,7 +32,7 @@ func CLIFlags() *portainer.CLIFlags {
|
||||
Assets: kingpin.Flag("assets", "Path to the assets").Default(defaultAssetsDirectory).Short('a').String(),
|
||||
Data: kingpin.Flag("data", "Path to the folder where the data is stored").Default(defaultDataDirectory).Short('d').String(),
|
||||
EndpointURL: kingpin.Flag("host", "Environment URL").Short('H').String(),
|
||||
FeatureFlags: kingpin.Flag("feat", "List of feature flags").Envar(portainer.FeatureFlagEnvVar).Strings(),
|
||||
FeatureFlags: kingpin.Flag("feat", "List of feature flags").Strings(),
|
||||
EnableEdgeComputeFeatures: kingpin.Flag("edge-compute", "Enable Edge Compute features").Bool(),
|
||||
NoAnalytics: kingpin.Flag("no-analytics", "Disable Analytics in app (deprecated)").Bool(),
|
||||
TLSSkipVerify: kingpin.Flag("tlsskipverify", "Disable TLS server verification").Default(defaultTLSSkipVerify).Bool(),
|
||||
@@ -52,12 +52,11 @@ func CLIFlags() *portainer.CLIFlags {
|
||||
SecretKeyName: kingpin.Flag("secret-key-name", "Secret key name for encryption and will be used as /run/secrets/<secret-key-name>.").Default(defaultSecretKeyName).String(),
|
||||
LogLevel: kingpin.Flag("log-level", "Set the minimum logging level to show").Default("INFO").Enum("DEBUG", "INFO", "WARN", "ERROR"),
|
||||
LogMode: kingpin.Flag("log-mode", "Set the logging output mode").Default("PRETTY").Enum("NOCOLOR", "PRETTY", "JSON"),
|
||||
KubectlShellImage: kingpin.Flag("kubectl-shell-image", "Kubectl shell image").Envar(portainer.KubectlShellImageEnvVar).Default(portainer.DefaultKubectlShellImage).String(),
|
||||
PullLimitCheckDisabled: kingpin.Flag("pull-limit-check-disabled", "Pull limit check").Envar(portainer.PullLimitCheckDisabledEnvVar).Default(defaultPullLimitCheckDisabled).Bool(),
|
||||
TrustedOrigins: kingpin.Flag("trusted-origins", "List of trusted origins for CSRF protection. Separate multiple origins with a comma.").Envar(portainer.TrustedOriginsEnvVar).String(),
|
||||
CSP: kingpin.Flag("csp", "Content Security Policy (CSP) header").Envar(portainer.CSPEnvVar).Default("true").Bool(),
|
||||
CompactDB: kingpin.Flag("compact-db", "Enable database compaction on startup").Envar(portainer.CompactDBEnvVar).Default("false").Bool(),
|
||||
NoSetupToken: kingpin.Flag("no-setup-token", "Disable the setup token requirement for admin initialization and restore on an uninitialized instance").Envar(portainer.NoSetupTokenEnvVar).Bool(),
|
||||
SetupToken: kingpin.Flag("setup-token", "Set a custom setup token for admin initialization and restore on an uninitialized instance (overrides auto-generation)").Envar(portainer.SetupTokenEnvVar).String(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,20 +95,8 @@ func (Service) ParseFlags(version string) (*portainer.CLIFlags, error) {
|
||||
flags.TLSKey = tlsKeyFlag.String()
|
||||
flags.TLSCacert = kingpin.Flag("tlscacert", "Path to the CA").Default(defaultTLSCACertPath).String()
|
||||
|
||||
var hasKubectlShellImageFlag bool
|
||||
kubectlShellImageFlag := kingpin.Flag(
|
||||
"kubectl-shell-image",
|
||||
"Kubectl shell image",
|
||||
).Envar(portainer.KubectlShellImageEnvVar).
|
||||
Default(portainer.DefaultKubectlShellImage).
|
||||
IsSetByUser(&hasKubectlShellImageFlag)
|
||||
flags.KubectlShellImage = kubectlShellImageFlag.String()
|
||||
|
||||
kingpin.Parse()
|
||||
|
||||
_, kubectlShellImageEnvVarSet := os.LookupEnv(portainer.KubectlShellImageEnvVar)
|
||||
flags.KubectlShellImageSet = hasKubectlShellImageFlag || kubectlShellImageEnvVarSet
|
||||
|
||||
if !filepath.IsAbs(*flags.Assets) {
|
||||
ex, err := os.Executable()
|
||||
if err != nil {
|
||||
@@ -161,11 +148,11 @@ func (Service) ParseFlags(version string) (*portainer.CLIFlags, error) {
|
||||
func (Service) ValidateFlags(flags *portainer.CLIFlags) error {
|
||||
displayDeprecationWarnings(flags)
|
||||
|
||||
if err := ValidateEndpointURL(*flags.EndpointURL); err != nil {
|
||||
if err := validateEndpointURL(*flags.EndpointURL); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := ValidateSnapshotInterval(*flags.SnapshotInterval); err != nil {
|
||||
if err := validateSnapshotInterval(*flags.SnapshotInterval); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -182,7 +169,7 @@ func displayDeprecationWarnings(flags *portainer.CLIFlags) {
|
||||
}
|
||||
}
|
||||
|
||||
func ValidateEndpointURL(endpointURL string) error {
|
||||
func validateEndpointURL(endpointURL string) error {
|
||||
if endpointURL == "" {
|
||||
return nil
|
||||
}
|
||||
@@ -207,7 +194,7 @@ func ValidateEndpointURL(endpointURL string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateSnapshotInterval(snapshotInterval string) error {
|
||||
func validateSnapshotInterval(snapshotInterval string) error {
|
||||
if snapshotInterval == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
zerolog "github.com/rs/zerolog/log"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -27,59 +26,6 @@ func TestOptionParser(t *testing.T) {
|
||||
require.True(t, *opts.EnableEdgeComputeFeatures)
|
||||
}
|
||||
|
||||
func TestParseKubectlShellImageFlag(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
args []string
|
||||
envVars map[string]string
|
||||
expectedKubectlShellImageSet bool
|
||||
expectedKubectlShellFlag string
|
||||
}{
|
||||
{
|
||||
name: "no flag, no env var",
|
||||
expectedKubectlShellImageSet: false,
|
||||
expectedKubectlShellFlag: portainer.DefaultKubectlShellImage,
|
||||
},
|
||||
{
|
||||
name: "explicit flag",
|
||||
args: []string{"portainer", "--kubectl-shell-image=myimage:v2"},
|
||||
expectedKubectlShellImageSet: true,
|
||||
expectedKubectlShellFlag: "myimage:v2",
|
||||
},
|
||||
{
|
||||
name: "env var",
|
||||
envVars: map[string]string{portainer.KubectlShellImageEnvVar: "myimage:v3"},
|
||||
expectedKubectlShellImageSet: true,
|
||||
expectedKubectlShellFlag: "myimage:v3",
|
||||
},
|
||||
{
|
||||
name: "both env var and flag set",
|
||||
args: []string{"portainer", "--kubectl-shell-image=myimage:v2"},
|
||||
envVars: map[string]string{portainer.KubectlShellImageEnvVar: "myimage:v3"},
|
||||
expectedKubectlShellImageSet: true,
|
||||
expectedKubectlShellFlag: "myimage:v2",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if tc.args == nil {
|
||||
tc.args = []string{"portainer"}
|
||||
}
|
||||
setOsArgs(t, tc.args)
|
||||
|
||||
for k, v := range tc.envVars {
|
||||
t.Setenv(k, v)
|
||||
}
|
||||
|
||||
flags, err := Service{}.ParseFlags("test-version")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expectedKubectlShellImageSet, flags.KubectlShellImageSet)
|
||||
require.Equal(t, tc.expectedKubectlShellFlag, *flags.KubectlShellImage)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseTLSFlags(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package cli
|
||||
|
||||
|
||||
@@ -4,17 +4,14 @@ import (
|
||||
"cmp"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
nethttp "net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/apikey"
|
||||
"github.com/portainer/portainer/api/chisel"
|
||||
"github.com/portainer/portainer/api/cli"
|
||||
"github.com/portainer/portainer/api/containerautomation"
|
||||
"github.com/portainer/portainer/api/crypto"
|
||||
"github.com/portainer/portainer/api/database"
|
||||
"github.com/portainer/portainer/api/database/boltdb"
|
||||
@@ -28,10 +25,10 @@ import (
|
||||
"github.com/portainer/portainer/api/exec"
|
||||
"github.com/portainer/portainer/api/filesystem"
|
||||
"github.com/portainer/portainer/api/git"
|
||||
"github.com/portainer/portainer/api/hostmanagement/openamt"
|
||||
"github.com/portainer/portainer/api/http"
|
||||
"github.com/portainer/portainer/api/http/proxy"
|
||||
kubeproxy "github.com/portainer/portainer/api/http/proxy/factory/kubernetes"
|
||||
"github.com/portainer/portainer/api/http/security/setuptoken"
|
||||
"github.com/portainer/portainer/api/internal/authorization"
|
||||
"github.com/portainer/portainer/api/internal/edge/edgestacks"
|
||||
"github.com/portainer/portainer/api/internal/endpointutils"
|
||||
@@ -54,16 +51,11 @@ import (
|
||||
"github.com/portainer/portainer/pkg/featureflags"
|
||||
"github.com/portainer/portainer/pkg/fips"
|
||||
"github.com/portainer/portainer/pkg/libhelm"
|
||||
"github.com/portainer/portainer/pkg/libhttp/ssrf"
|
||||
libhelmtypes "github.com/portainer/portainer/pkg/libhelm/types"
|
||||
"github.com/portainer/portainer/pkg/libstack/compose"
|
||||
libswarm "github.com/portainer/portainer/pkg/libstack/swarm"
|
||||
"github.com/portainer/portainer/pkg/validate"
|
||||
|
||||
gogitclient "github.com/go-git/go-git/v5/plumbing/transport/client"
|
||||
gogitraw "github.com/go-git/go-git/v5/plumbing/transport/git"
|
||||
gogithttp "github.com/go-git/go-git/v5/plumbing/transport/http"
|
||||
gogitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
|
||||
"github.com/google/uuid"
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
@@ -127,7 +119,7 @@ func initDataStore(flags *portainer.CLIFlags, secretKey []byte, fileService port
|
||||
}
|
||||
|
||||
if isNew {
|
||||
instanceId, err := uuid.NewRandom()
|
||||
instanceId, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("failed generating instance id")
|
||||
}
|
||||
@@ -142,16 +134,15 @@ func initDataStore(flags *portainer.CLIFlags, secretKey []byte, fileService port
|
||||
InstanceID: instanceId.String(),
|
||||
MigratorCount: migratorCount,
|
||||
}
|
||||
|
||||
if err := store.VersionService.UpdateVersion(&v); err != nil {
|
||||
log.Fatal().Err(err).Msg("failed to update version")
|
||||
}
|
||||
store.VersionService.UpdateVersion(&v)
|
||||
|
||||
if err := updateSettingsFromFlags(store, flags); err != nil {
|
||||
log.Fatal().Err(err).Msg("failed updating settings from flags")
|
||||
}
|
||||
} else if err := store.MigrateData(); err != nil {
|
||||
log.Fatal().Err(err).Msg("failed migration")
|
||||
} else {
|
||||
if err := store.MigrateData(); err != nil {
|
||||
log.Fatal().Err(err).Msg("failed migration")
|
||||
}
|
||||
}
|
||||
|
||||
if err := updateSettingsFromFlags(store, flags); err != nil {
|
||||
@@ -162,7 +153,7 @@ func initDataStore(flags *portainer.CLIFlags, secretKey []byte, fileService port
|
||||
go func() {
|
||||
<-shutdownCtx.Done()
|
||||
|
||||
defer logs.CloseAndLogErr(connection)
|
||||
defer connection.Close()
|
||||
}()
|
||||
|
||||
return store
|
||||
@@ -182,6 +173,10 @@ func initKubernetesDeployer(kubernetesTokenCacheManager *kubeproxy.TokenCacheMan
|
||||
return exec.NewKubernetesDeployer(kubernetesTokenCacheManager, kubernetesClientFactory, dataStore, reverseTunnelService, signatureService, proxyManager)
|
||||
}
|
||||
|
||||
func initHelmPackageManager() (libhelmtypes.HelmPackageManager, error) {
|
||||
return libhelm.NewHelmPackageManager()
|
||||
}
|
||||
|
||||
func initAPIKeyService(datastore dataservices.DataStore) apikey.APIKeyService {
|
||||
return apikey.NewAPIKeyService(datastore.APIKeyRepository(), datastore.User())
|
||||
}
|
||||
@@ -220,12 +215,13 @@ func initSnapshotService(
|
||||
dataStore dataservices.DataStore,
|
||||
dockerClientFactory *dockerclient.ClientFactory,
|
||||
kubernetesClientFactory *kubecli.ClientFactory,
|
||||
shutdownCtx context.Context,
|
||||
pendingActionsService *pendingactions.PendingActionsService,
|
||||
) (portainer.SnapshotService, error) {
|
||||
dockerSnapshotter := docker.NewSnapshotter(dockerClientFactory)
|
||||
kubernetesSnapshotter := kubernetes.NewSnapshotter(kubernetesClientFactory)
|
||||
|
||||
snapshotService, err := snapshot.NewService(snapshotIntervalFromFlag, dataStore, dockerSnapshotter, kubernetesSnapshotter, pendingActionsService)
|
||||
snapshotService, err := snapshot.NewService(snapshotIntervalFromFlag, dataStore, dockerSnapshotter, kubernetesSnapshotter, shutdownCtx, pendingActionsService)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -233,32 +229,6 @@ func initSnapshotService(
|
||||
return snapshotService, nil
|
||||
}
|
||||
|
||||
func resolveSetupToken(tx dataservices.DataStoreTx, providedToken string) (string, error) {
|
||||
admins, err := tx.User().UsersByRole(portainer.AdministratorRole)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(admins) > 0 {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
if providedToken != "" {
|
||||
log.Info().Msg("using custom setup token; admin initialization and backup restore require this token in the X-Setup-Token header")
|
||||
return providedToken, nil
|
||||
}
|
||||
|
||||
token, err := setuptoken.Generate()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
log.Info().
|
||||
Str("setup_token", token).
|
||||
Msg("no administrator account configured; admin initialization and backup restore require this setup token in the X-Setup-Token header. Start with --no-setup-token to disable.")
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func initStatus(instanceID string) *portainer.Status {
|
||||
return &portainer.Status{
|
||||
Version: portainer.APIVersion,
|
||||
@@ -277,10 +247,6 @@ func updateSettingsFromFlags(dataStore dataservices.DataStore, flags *portainer.
|
||||
settings.EnableEdgeComputeFeatures = cmp.Or(*flags.EnableEdgeComputeFeatures, settings.EnableEdgeComputeFeatures)
|
||||
settings.TemplatesURL = cmp.Or(*flags.Templates, settings.TemplatesURL)
|
||||
|
||||
if flags.KubectlShellImageSet {
|
||||
settings.KubectlShellImage = *flags.KubectlShellImage
|
||||
}
|
||||
|
||||
if *flags.Labels != nil {
|
||||
settings.BlackListedLabels = *flags.Labels
|
||||
}
|
||||
@@ -371,7 +337,9 @@ func loadEncryptionSecretKey(keyfilename string) []byte {
|
||||
return hash[:]
|
||||
}
|
||||
|
||||
func buildServer(flags *portainer.CLIFlags, shutdownCtx context.Context, shutdownTrigger context.CancelFunc) portainer.Server {
|
||||
func buildServer(flags *portainer.CLIFlags) portainer.Server {
|
||||
shutdownCtx, shutdownTrigger := context.WithCancel(context.Background())
|
||||
|
||||
if flags.FeatureFlags != nil {
|
||||
featureflags.Parse(*flags.FeatureFlags, portainer.SupportedFeatureFlags)
|
||||
}
|
||||
@@ -379,9 +347,9 @@ func buildServer(flags *portainer.CLIFlags, shutdownCtx context.Context, shutdow
|
||||
trustedOrigins := []string{}
|
||||
if *flags.TrustedOrigins != "" {
|
||||
// validate if the trusted origins are valid urls
|
||||
for origin := range strings.SplitSeq(*flags.TrustedOrigins, ",") {
|
||||
for _, origin := range strings.Split(*flags.TrustedOrigins, ",") {
|
||||
if !validate.IsTrustedOrigin(origin) {
|
||||
log.Fatal().Str("trusted_origin", origin).Msg("invalid trusted origin: must be scheme://host or scheme://host:port (e.g. https://example.com)")
|
||||
log.Fatal().Str("trusted_origin", origin).Msg("invalid url for trusted origin. Please check the trusted origins flag.")
|
||||
}
|
||||
|
||||
trustedOrigins = append(trustedOrigins, origin)
|
||||
@@ -408,19 +376,6 @@ func buildServer(flags *portainer.CLIFlags, shutdownCtx context.Context, shutdow
|
||||
log.Fatal().Msg("The database schema version does not align with the server version. Please consider reverting to the previous server version or addressing the database migration issue.")
|
||||
}
|
||||
|
||||
if err := ssrf.Configure(dataStore.AllowList()); err != nil {
|
||||
log.Fatal().Err(err).Msg("failed initializing ssrf service")
|
||||
}
|
||||
|
||||
if !ssrf.WrapDefaultTransport() {
|
||||
log.Fatal().Msg("failed to wrap default HTTP transport with SSRF protection")
|
||||
}
|
||||
|
||||
gogithttp.DefaultClient = gogithttp.NewClient(&nethttp.Client{Transport: nethttp.DefaultTransport})
|
||||
gogitclient.InstallProtocol("git", git.NewSSRFGitTransport(gogitraw.DefaultClient))
|
||||
gogitclient.InstallProtocol("ssh", git.NewSSRFGitTransport(gogitssh.DefaultClient))
|
||||
gogitclient.InstallProtocol("file", nil)
|
||||
|
||||
instanceID, err := dataStore.Version().InstanceID()
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("failed getting instance id")
|
||||
@@ -444,6 +399,9 @@ func buildServer(flags *portainer.CLIFlags, shutdownCtx context.Context, shutdow
|
||||
|
||||
gitService := git.NewService(shutdownCtx)
|
||||
|
||||
// Setting insecureSkipVerify to true to preserve the old behaviour.
|
||||
openAMTService := openamt.NewService(true)
|
||||
|
||||
cryptoService := crypto.Service{}
|
||||
|
||||
signatureService := initDigitalSignatureService()
|
||||
@@ -484,11 +442,16 @@ func buildServer(flags *portainer.CLIFlags, shutdownCtx context.Context, shutdow
|
||||
|
||||
reverseTunnelService.ProxyManager = proxyManager
|
||||
|
||||
dockerConfigPath := fileService.GetDockerConfigPath()
|
||||
|
||||
composeDeployer := compose.NewComposeDeployer()
|
||||
|
||||
composeStackManager := exec.NewComposeStackManager(composeDeployer, proxyManager)
|
||||
composeStackManager := exec.NewComposeStackManager(composeDeployer, proxyManager, dataStore)
|
||||
|
||||
swarmStackManager := exec.NewSwarmStackManager(libswarm.NewSwarmDeployer(), proxyManager)
|
||||
swarmStackManager, err := exec.NewSwarmStackManager(*flags.Assets, dockerConfigPath, signatureService, fileService, reverseTunnelService, dataStore)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("failed initializing swarm stack manager")
|
||||
}
|
||||
|
||||
kubernetesDeployer := initKubernetesDeployer(kubernetesTokenCacheManager, kubernetesClientFactory, dataStore, reverseTunnelService, signatureService, proxyManager)
|
||||
|
||||
@@ -497,16 +460,19 @@ func buildServer(flags *portainer.CLIFlags, shutdownCtx context.Context, shutdow
|
||||
pendingActionsService.RegisterHandler(actions.DeletePortainerK8sRegistrySecrets, handlers.NewHandlerDeleteRegistrySecrets(authorizationService, dataStore, kubernetesClientFactory))
|
||||
pendingActionsService.RegisterHandler(actions.PostInitMigrateEnvironment, handlers.NewHandlerPostInitMigrateEnvironment(authorizationService, dataStore, kubernetesClientFactory, dockerClientFactory, *flags.Assets, kubernetesDeployer))
|
||||
|
||||
snapshotService, err := initSnapshotService(*flags.SnapshotInterval, dataStore, dockerClientFactory, kubernetesClientFactory, pendingActionsService)
|
||||
snapshotService, err := initSnapshotService(*flags.SnapshotInterval, dataStore, dockerClientFactory, kubernetesClientFactory, shutdownCtx, pendingActionsService)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("failed initializing snapshot service")
|
||||
}
|
||||
|
||||
snapshotService.Start(shutdownCtx)
|
||||
snapshotService.Start()
|
||||
|
||||
proxyManager.NewProxyFactory(dataStore, signatureService, reverseTunnelService, dockerClientFactory, kubernetesClientFactory, kubernetesTokenCacheManager, gitService, snapshotService, jwtService)
|
||||
|
||||
helmPackageManager := libhelm.NewHelmPackageManager()
|
||||
helmPackageManager, err := initHelmPackageManager()
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("failed initializing helm package manager")
|
||||
}
|
||||
|
||||
applicationStatus := initStatus(instanceID)
|
||||
|
||||
@@ -557,37 +523,23 @@ func buildServer(flags *portainer.CLIFlags, shutdownCtx context.Context, shutdow
|
||||
}
|
||||
}
|
||||
|
||||
setupToken := ""
|
||||
if adminPasswordHash == "" && !*flags.NoSetupToken {
|
||||
if err := dataStore.ViewTx(func(tx dataservices.DataStoreTx) error {
|
||||
var txErr error
|
||||
setupToken, txErr = resolveSetupToken(tx, *flags.SetupToken)
|
||||
return txErr
|
||||
}); err != nil {
|
||||
log.Fatal().Err(err).Msg("failed initializing setup token")
|
||||
}
|
||||
}
|
||||
|
||||
if err := reverseTunnelService.StartTunnelServer(*flags.TunnelAddr, *flags.TunnelPort, snapshotService); err != nil {
|
||||
log.Fatal().Err(err).Msg("failed starting tunnel server")
|
||||
}
|
||||
|
||||
scheduler := scheduler.NewScheduler(shutdownCtx)
|
||||
stackDeployer := deployments.NewStackDeployer(swarmStackManager, composeStackManager, kubernetesDeployer, dockerClientFactory, dataStore)
|
||||
if err := deployments.StartStackSchedules(scheduler, stackDeployer, dataStore, gitService); err != nil {
|
||||
log.Fatal().Err(err).Msg("failed to start stack scheduler")
|
||||
}
|
||||
|
||||
containerService := docker.NewContainerService(dockerClientFactory, dataStore)
|
||||
containerAutomationService := containerautomation.NewService(shutdownCtx, scheduler, dataStore, dockerClientFactory, containerService, stackDeployer)
|
||||
containerAutomationService.Start()
|
||||
deployments.StartStackSchedules(scheduler, stackDeployer, dataStore, gitService)
|
||||
|
||||
sslDBSettings, err := dataStore.SSLSettings().Settings()
|
||||
if err != nil {
|
||||
log.Fatal().Msg("failed to fetch SSL settings from DB")
|
||||
}
|
||||
|
||||
platformService := platform.NewService(dataStore)
|
||||
platformService, err := platform.NewService(dataStore)
|
||||
if err != nil {
|
||||
log.Fatal().Err(err).Msg("failed initializing platform service")
|
||||
}
|
||||
|
||||
upgradeService, err := upgrade.NewService(
|
||||
*flags.Assets,
|
||||
@@ -617,13 +569,6 @@ func buildServer(flags *portainer.CLIFlags, shutdownCtx context.Context, shutdow
|
||||
log.Fatal().Err(err).Msg("failure during post init migrations")
|
||||
}
|
||||
|
||||
if err := dataStore.UpdateTx(func(tx dataservices.DataStoreTx) error {
|
||||
return recoverStaleDeployingStacks(tx)
|
||||
}); err != nil {
|
||||
log.Info().Err(err).
|
||||
Msg("Error recovering stale deploying stacks")
|
||||
}
|
||||
|
||||
return &http.Server{
|
||||
AuthorizationService: authorizationService,
|
||||
ReverseTunnelService: reverseTunnelService,
|
||||
@@ -646,6 +591,7 @@ func buildServer(flags *portainer.CLIFlags, shutdownCtx context.Context, shutdow
|
||||
LDAPService: ldapService,
|
||||
OAuthService: oauthService,
|
||||
GitService: gitService,
|
||||
OpenAMTService: openAMTService,
|
||||
ProxyManager: proxyManager,
|
||||
KubernetesTokenCacheManager: kubernetesTokenCacheManager,
|
||||
KubeClusterAccessService: kubeClusterAccessService,
|
||||
@@ -655,7 +601,7 @@ func buildServer(flags *portainer.CLIFlags, shutdownCtx context.Context, shutdow
|
||||
DockerClientFactory: dockerClientFactory,
|
||||
KubernetesClientFactory: kubernetesClientFactory,
|
||||
Scheduler: scheduler,
|
||||
ContainerAutomationService: containerAutomationService,
|
||||
ShutdownCtx: shutdownCtx,
|
||||
ShutdownTrigger: shutdownTrigger,
|
||||
StackDeployer: stackDeployer,
|
||||
UpgradeService: upgradeService,
|
||||
@@ -664,7 +610,6 @@ func buildServer(flags *portainer.CLIFlags, shutdownCtx context.Context, shutdow
|
||||
PlatformService: platformService,
|
||||
PullLimitCheckDisabled: *flags.PullLimitCheckDisabled,
|
||||
TrustedOrigins: trustedOrigins,
|
||||
SetupToken: setupToken,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -678,57 +623,20 @@ func main() {
|
||||
logs.SetLoggingMode(*flags.LogMode)
|
||||
|
||||
for {
|
||||
shutdownCtx, shutdownTrigger := context.WithCancel(context.Background())
|
||||
server := buildServer(flags, shutdownCtx, shutdownTrigger)
|
||||
server := buildServer(flags)
|
||||
|
||||
log.Info().
|
||||
Str("version", portainer.APIVersion).
|
||||
Str("build_number", build.BuildNumber).
|
||||
Str("image_tag", build.ImageTag).
|
||||
Str("nodejs_version", build.NodejsVersion).
|
||||
Str("pnpm_version", build.PnpmVersion).
|
||||
Str("yarn_version", build.YarnVersion).
|
||||
Str("webpack_version", build.WebpackVersion).
|
||||
Str("go_version", build.GoVersion).
|
||||
Msg("starting Portainer")
|
||||
|
||||
err := server.Start(shutdownCtx)
|
||||
err := server.Start()
|
||||
|
||||
log.Info().Err(err).Msg("HTTP server exited")
|
||||
}
|
||||
}
|
||||
|
||||
// recoverStaleDeployingStacks resets any stack that was left in the Deploying state
|
||||
// (e.g. because the server was restarted mid-deployment) to the Error state so the
|
||||
// user can retry.
|
||||
func recoverStaleDeployingStacks(tx dataservices.DataStoreTx) error {
|
||||
stacks, err := tx.Stack().ReadAll(func(s portainer.Stack) bool {
|
||||
return s.Status == portainer.StackStatusDeploying
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, stack := range stacks {
|
||||
stack.Status = portainer.StackStatusError
|
||||
stack.DeploymentStatus = append(stack.DeploymentStatus, portainer.StackDeploymentStatus{
|
||||
Status: portainer.StackStatusError,
|
||||
Time: time.Now().Unix(),
|
||||
Message: "Deployment interrupted by server restart",
|
||||
})
|
||||
|
||||
if err := tx.Stack().Update(stack.ID, &stack); err != nil {
|
||||
log.Warn().Err(err).
|
||||
Int("stack_id", int(stack.ID)).
|
||||
Str("context", "RecoverStaleDeployingStacks").
|
||||
Msg("Unable to recover stale deploying stack")
|
||||
continue
|
||||
}
|
||||
log.Debug().
|
||||
Int("stack_id", int(stack.ID)).
|
||||
Str("stack_name", stack.Name).
|
||||
Str("context", "RecoverStaleDeployingStacks").
|
||||
Msg("Recovered stale deploying stack to error state")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,66 +2,24 @@ package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/filesystem"
|
||||
"github.com/portainer/portainer/api/internal/testhelpers"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_resolveSetupToken(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("admin already exists — returns empty token", func(t *testing.T) {
|
||||
admin := portainer.User{Role: portainer.AdministratorRole}
|
||||
store := testhelpers.NewDatastore(testhelpers.WithUsers([]portainer.User{admin}))
|
||||
token, err := resolveSetupToken(store, "")
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, token)
|
||||
})
|
||||
|
||||
t.Run("no admin — generates a 64-char hex token", func(t *testing.T) {
|
||||
store := testhelpers.NewDatastore(testhelpers.WithUsers([]portainer.User{}))
|
||||
token, err := resolveSetupToken(store, "")
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, token, 64)
|
||||
|
||||
token2, err := resolveSetupToken(store, "")
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, token, token2)
|
||||
})
|
||||
|
||||
t.Run("no admin — uses provided token", func(t *testing.T) {
|
||||
store := testhelpers.NewDatastore(testhelpers.WithUsers([]portainer.User{}))
|
||||
token, err := resolveSetupToken(store, "mysecrettoken")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "mysecrettoken", token)
|
||||
})
|
||||
|
||||
t.Run("admin already exists — ignores provided token", func(t *testing.T) {
|
||||
admin := portainer.User{Role: portainer.AdministratorRole}
|
||||
store := testhelpers.NewDatastore(testhelpers.WithUsers([]portainer.User{admin}))
|
||||
token, err := resolveSetupToken(store, "mysecrettoken")
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, token)
|
||||
})
|
||||
}
|
||||
|
||||
const secretFileName = "secret.txt"
|
||||
|
||||
func createPasswordFile(t *testing.T, secretPath, password string) string {
|
||||
err := os.WriteFile(secretPath, []byte(password), 0o600)
|
||||
err := os.WriteFile(secretPath, []byte(password), 0600)
|
||||
require.NoError(t, err)
|
||||
return secretPath
|
||||
}
|
||||
|
||||
func TestLoadEncryptionSecretKey(t *testing.T) {
|
||||
t.Parallel()
|
||||
tempDir := t.TempDir()
|
||||
secretPath := filesystem.JoinPaths(tempDir, secretFileName)
|
||||
secretPath := path.Join(tempDir, secretFileName)
|
||||
|
||||
// first pointing to file that does not exist, gives nil hash (no encryption)
|
||||
encryptionKey := loadEncryptionSecretKey(secretPath)
|
||||
@@ -80,67 +38,7 @@ func TestLoadEncryptionSecretKey(t *testing.T) {
|
||||
require.Len(t, encryptionKey, 32)
|
||||
}
|
||||
|
||||
func TestUpdateSettingsFromFlags_KubectlShellImage(t *testing.T) {
|
||||
const existingImage = "existing-image:v1"
|
||||
const newImage = "new-image:v2"
|
||||
|
||||
emptyString := ""
|
||||
falseBool := false
|
||||
var emptyLabels []portainer.Pair
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
imageSet bool
|
||||
flagImage string
|
||||
expectedKubectlShellImage string
|
||||
}{
|
||||
{
|
||||
name: "flag not set — DB image unchanged",
|
||||
imageSet: false,
|
||||
flagImage: portainer.DefaultKubectlShellImage,
|
||||
expectedKubectlShellImage: existingImage,
|
||||
},
|
||||
{
|
||||
name: "flag set — DB image updated",
|
||||
imageSet: true,
|
||||
flagImage: newImage,
|
||||
expectedKubectlShellImage: newImage,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
store := testhelpers.NewDatastore(
|
||||
testhelpers.WithSettingsService(&portainer.Settings{
|
||||
KubectlShellImage: existingImage,
|
||||
}),
|
||||
testhelpers.WithSSLSettingsService(&portainer.SSLSettings{}),
|
||||
)
|
||||
|
||||
flags := &portainer.CLIFlags{
|
||||
SnapshotInterval: &emptyString,
|
||||
Logo: &emptyString,
|
||||
EnableEdgeComputeFeatures: &falseBool,
|
||||
Templates: &emptyString,
|
||||
Labels: &emptyLabels,
|
||||
HTTPDisabled: &falseBool,
|
||||
HTTPEnabled: &falseBool,
|
||||
}
|
||||
flags.KubectlShellImage = &tc.flagImage
|
||||
flags.KubectlShellImageSet = tc.imageSet
|
||||
|
||||
err := updateSettingsFromFlags(store, flags)
|
||||
require.NoError(t, err)
|
||||
|
||||
settings, err := store.Settings().Settings()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expectedKubectlShellImage, settings.KubectlShellImage)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDBSecretPath(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
keyFilenameFlag string
|
||||
expected string
|
||||
|
||||
@@ -1,149 +0,0 @@
|
||||
package concurrent
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestRun_AllSucceed(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
fn1 := func(ctx context.Context) (any, error) { return "one", nil }
|
||||
fn2 := func(ctx context.Context) (any, error) { return "two", nil }
|
||||
fn3 := func(ctx context.Context) (any, error) { return "three", nil }
|
||||
|
||||
results, err := Run(t.Context(), 0, fn1, fn2, fn3)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Len(t, results, 3)
|
||||
|
||||
values := make([]string, 0, len(results))
|
||||
for _, r := range results {
|
||||
values = append(values, r.Result.(string))
|
||||
}
|
||||
require.ElementsMatch(t, []string{"one", "two", "three"}, values)
|
||||
}
|
||||
|
||||
func TestRun_OneError(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
sentinel := errors.New("task failed")
|
||||
|
||||
fn1 := func(ctx context.Context) (any, error) { return "ok", nil }
|
||||
fn2 := func(ctx context.Context) (any, error) { return nil, sentinel }
|
||||
|
||||
_, err := Run(t.Context(), 0, fn1, fn2)
|
||||
|
||||
require.ErrorIs(t, err, sentinel)
|
||||
}
|
||||
|
||||
func TestRun_NoTasks(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
results, err := Run(t.Context(), 0)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, results)
|
||||
}
|
||||
|
||||
func TestRun_MaxConcurrency(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const numTasks = 10
|
||||
var peak atomic.Int32
|
||||
var active atomic.Int32
|
||||
|
||||
task := func(ctx context.Context) (any, error) {
|
||||
current := active.Add(1)
|
||||
if current > peak.Load() {
|
||||
peak.Store(current)
|
||||
}
|
||||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
active.Add(-1)
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
tasks := make([]Func, numTasks)
|
||||
for i := range tasks {
|
||||
tasks[i] = task
|
||||
}
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
results, err := Run(t.Context(), 3, tasks...)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, results, numTasks)
|
||||
require.LessOrEqual(t, peak.Load(), int32(3))
|
||||
})
|
||||
}
|
||||
|
||||
func TestRun_ZeroConcurrencyUsesAllTasks(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
const numTasks = 5
|
||||
var peak atomic.Int32
|
||||
var active atomic.Int32
|
||||
|
||||
task := func(ctx context.Context) (any, error) {
|
||||
current := active.Add(1)
|
||||
if current > peak.Load() {
|
||||
peak.Store(current)
|
||||
}
|
||||
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
active.Add(-1)
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
tasks := make([]Func, numTasks)
|
||||
for i := range tasks {
|
||||
tasks[i] = task
|
||||
}
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
results, err := Run(t.Context(), 0, tasks...)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, results, numTasks)
|
||||
require.Equal(t, int32(numTasks), peak.Load())
|
||||
})
|
||||
}
|
||||
|
||||
func TestRun_ContextCancelledBeforeStart(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
cancel()
|
||||
|
||||
called := atomic.Bool{}
|
||||
fn := func(ctx context.Context) (any, error) {
|
||||
called.Store(true)
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
|
||||
_, err := Run(ctx, 1, fn, fn, fn)
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestRun_ContextPassedToTasks(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
type key struct{}
|
||||
ctx := context.WithValue(t.Context(), key{}, "testvalue")
|
||||
|
||||
fn := func(ctx context.Context) (any, error) {
|
||||
return ctx.Value(key{}), nil
|
||||
}
|
||||
|
||||
results, err := Run(ctx, 0, fn)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "testvalue", results[0].Result)
|
||||
}
|
||||
@@ -46,7 +46,7 @@ type Connection interface {
|
||||
|
||||
IsEncryptedStore() bool
|
||||
NeedsEncryptionMigration() (bool, error)
|
||||
SetEncrypted(encrypted bool) error
|
||||
SetEncrypted(encrypted bool)
|
||||
|
||||
BackupMetadata() (map[string]any, error)
|
||||
RestoreMetadata(s map[string]any) error
|
||||
|
||||
@@ -1,190 +0,0 @@
|
||||
package containerautomation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/internal/endpointutils"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
const (
|
||||
// retryWindow is the rolling window over which max restarts per container are counted.
|
||||
retryWindow = 10 * time.Minute
|
||||
// restartCooldown is the minimum delay between two restarts of the same container,
|
||||
// giving its healthcheck time to recover before we try again.
|
||||
restartCooldown = 60 * time.Second
|
||||
// endpointTimeout bounds the container-list call for a single endpoint.
|
||||
endpointTimeout = 30 * time.Second
|
||||
// restartTimeoutBuffer is added on top of a container's stop-timeout to derive
|
||||
// the deadline of its own restart context, leaving room for the engine to kill
|
||||
// and start the container after the graceful stop window elapses.
|
||||
restartTimeoutBuffer = 15 * time.Second
|
||||
)
|
||||
|
||||
// retryState tracks restart accounting for a single container across ticks.
|
||||
type retryState struct {
|
||||
attempts int
|
||||
windowStart time.Time
|
||||
lastRestart time.Time
|
||||
}
|
||||
|
||||
// retryPolicy holds the cooldown/window parameters applied to a container.
|
||||
type retryPolicy struct {
|
||||
maxRetries int
|
||||
window time.Duration
|
||||
cooldown time.Duration
|
||||
}
|
||||
|
||||
// decideRestart is a pure function that decides whether an unhealthy container
|
||||
// should be restarted now, given its current retry state and policy. It returns
|
||||
// the decision and the updated state to persist.
|
||||
//
|
||||
// Rules, in order:
|
||||
// - reset the window (and attempts) when the window has elapsed;
|
||||
// - deny while still within the cooldown since the last restart;
|
||||
// - deny once the max number of restarts in the current window is reached;
|
||||
// - otherwise restart, incrementing the attempt counter.
|
||||
func decideRestart(state retryState, policy retryPolicy, now time.Time) (bool, retryState) {
|
||||
if state.windowStart.IsZero() || now.Sub(state.windowStart) >= policy.window {
|
||||
state.windowStart = now
|
||||
state.attempts = 0
|
||||
}
|
||||
|
||||
if !state.lastRestart.IsZero() && now.Sub(state.lastRestart) < policy.cooldown {
|
||||
return false, state
|
||||
}
|
||||
|
||||
if state.attempts >= policy.maxRetries {
|
||||
return false, state
|
||||
}
|
||||
|
||||
state.attempts++
|
||||
state.lastRestart = now
|
||||
|
||||
return true, state
|
||||
}
|
||||
|
||||
// heal runs a single auto-heal pass over every reachable Docker endpoint.
|
||||
// It is registered with the scheduler and guarded against overlapping ticks by
|
||||
// the Service. Errors are logged per endpoint/container so one failure does not
|
||||
// abort the whole pass; it always returns nil so the scheduler keeps the job.
|
||||
func (s *Service) heal() error {
|
||||
if !s.running.CompareAndSwap(false, true) {
|
||||
log.Debug().Msg("auto-heal: previous run still in progress, skipping tick")
|
||||
return nil
|
||||
}
|
||||
defer s.running.Store(false)
|
||||
|
||||
scope := s.scope()
|
||||
|
||||
endpoints, err := s.dataStore.Endpoint().Endpoints()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("auto-heal: unable to list environments")
|
||||
return nil
|
||||
}
|
||||
|
||||
for i := range endpoints {
|
||||
endpoint := &endpoints[i]
|
||||
|
||||
// M1 scope: native Docker endpoints only. Kubernetes is not applicable and
|
||||
// Edge/async endpoints are not reachable synchronously from the scheduler.
|
||||
if !endpointutils.IsDockerEndpoint(endpoint) || endpointutils.IsEdgeEndpoint(endpoint) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Per-endpoint opt-out (M5): skip environments where automation is disabled,
|
||||
// independently of the global switch. Zero value participates, so existing
|
||||
// installs are unaffected.
|
||||
if !AutomationEnabledForEndpoint(endpoint) {
|
||||
log.Debug().Int("endpoint_id", int(endpoint.ID)).
|
||||
Msg("auto-heal: automation disabled for this environment, skipping")
|
||||
continue
|
||||
}
|
||||
|
||||
s.healEndpoint(endpoint, scope)
|
||||
}
|
||||
|
||||
// Drop retry state only for containers whose retry window has fully elapsed
|
||||
// since their last restart. A container that briefly leaves the unhealthy
|
||||
// filter (e.g. while "starting" after a restart) keeps its accounting, so the
|
||||
// cooldown / max-retries storm guard survives flapping.
|
||||
s.pruneRetries(time.Now())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// healEndpoint restarts the in-scope unhealthy containers of a single endpoint.
|
||||
func (s *Service) healEndpoint(endpoint *portainer.Endpoint, scope string) {
|
||||
endpointID := int(endpoint.ID)
|
||||
|
||||
// Swarm note (M1 limitation): we connect to the endpoint's primary node only
|
||||
// (nodeName ""). Containers scheduled on other Swarm nodes are not healed here;
|
||||
// per-node iteration is deferred to a later milestone.
|
||||
clientTimeout := endpointTimeout
|
||||
cli, err := s.clientFactory.CreateClient(endpoint, "", &clientTimeout)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Int("endpoint_id", endpointID).Msg("auto-heal: unable to create Docker client")
|
||||
return
|
||||
}
|
||||
defer cli.Close()
|
||||
|
||||
listCtx, cancel := context.WithTimeout(s.baseCtx, endpointTimeout)
|
||||
defer cancel()
|
||||
|
||||
// List running unhealthy containers only (All:false). Docker keeps
|
||||
// Health.Status=="unhealthy" on stopped containers, so listing with All:true
|
||||
// would let us "restart" (i.e. start) an intentionally-stopped container.
|
||||
listFilters := filters.NewArgs(filters.Arg("health", "unhealthy"))
|
||||
containers, err := cli.ContainerList(listCtx, container.ListOptions{All: false, Filters: listFilters})
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Int("endpoint_id", endpointID).Msg("auto-heal: unable to list containers")
|
||||
return
|
||||
}
|
||||
|
||||
for _, c := range containers {
|
||||
if !InScope(scope, c.Labels) {
|
||||
continue
|
||||
}
|
||||
|
||||
policy := retryPolicy{
|
||||
maxRetries: MaxRetries(c.Labels),
|
||||
window: retryWindow,
|
||||
cooldown: restartCooldown,
|
||||
}
|
||||
|
||||
ok, newState := decideRestart(s.getRetry(c.ID), policy, time.Now())
|
||||
s.setRetry(c.ID, newState)
|
||||
if !ok {
|
||||
log.Debug().Str("container_id", c.ID).Int("endpoint_id", endpointID).
|
||||
Msg("auto-heal: restart skipped (cooldown or max retries reached)")
|
||||
continue
|
||||
}
|
||||
|
||||
timeout := StopTimeout(c.Labels)
|
||||
|
||||
// Each restart gets its own context, bounded by the container's stop-timeout
|
||||
// plus a buffer, so one slow restart cannot starve the others and a hung
|
||||
// engine call is bounded independently of the list deadline.
|
||||
restartTimeout := time.Duration(timeout)*time.Second + restartTimeoutBuffer
|
||||
restartCtx, restartCancel := context.WithTimeout(s.baseCtx, restartTimeout)
|
||||
err := cli.ContainerRestart(restartCtx, c.ID, container.StopOptions{Timeout: &timeout})
|
||||
restartCancel()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Str("container_id", c.ID).Int("endpoint_id", endpointID).
|
||||
Msg("auto-heal: failed to restart unhealthy container")
|
||||
continue
|
||||
}
|
||||
|
||||
log.Info().Str("container_id", c.ID).Int("endpoint_id", endpointID).Int("attempt", newState.attempts).
|
||||
Msg("auto-heal: restarted unhealthy container")
|
||||
s.notifier.Notify(Event{
|
||||
Kind: EventHealRestarted, EndpointID: endpointID, ContainerID: c.ID, ContainerName: containerName(c.Names),
|
||||
Message: "restarted unhealthy container",
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,137 +0,0 @@
|
||||
package containerautomation
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestDecideRestart(t *testing.T) {
|
||||
policy := retryPolicy{
|
||||
maxRetries: 3,
|
||||
window: 10 * time.Minute,
|
||||
cooldown: 60 * time.Second,
|
||||
}
|
||||
base := time.Date(2026, 6, 28, 12, 0, 0, 0, time.UTC)
|
||||
|
||||
t.Run("first restart on empty state", func(t *testing.T) {
|
||||
ok, state := decideRestart(retryState{}, policy, base)
|
||||
if !ok {
|
||||
t.Fatal("expected restart on first unhealthy observation")
|
||||
}
|
||||
if state.attempts != 1 {
|
||||
t.Errorf("attempts = %d, want 1", state.attempts)
|
||||
}
|
||||
if !state.windowStart.Equal(base) || !state.lastRestart.Equal(base) {
|
||||
t.Error("windowStart/lastRestart should be set to now")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("blocked during cooldown", func(t *testing.T) {
|
||||
_, state := decideRestart(retryState{}, policy, base)
|
||||
ok, _ := decideRestart(state, policy, base.Add(30*time.Second))
|
||||
if ok {
|
||||
t.Error("expected restart to be blocked within cooldown")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("allowed after cooldown", func(t *testing.T) {
|
||||
_, state := decideRestart(retryState{}, policy, base)
|
||||
ok, state := decideRestart(state, policy, base.Add(61*time.Second))
|
||||
if !ok {
|
||||
t.Error("expected restart allowed after cooldown")
|
||||
}
|
||||
if state.attempts != 2 {
|
||||
t.Errorf("attempts = %d, want 2", state.attempts)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("max retries enforced within window", func(t *testing.T) {
|
||||
state := retryState{}
|
||||
now := base
|
||||
allowed := 0
|
||||
for i := 0; i < 6; i++ {
|
||||
ok, newState := decideRestart(state, policy, now)
|
||||
state = newState
|
||||
if ok {
|
||||
allowed++
|
||||
}
|
||||
now = now.Add(policy.cooldown + time.Second)
|
||||
}
|
||||
if allowed != policy.maxRetries {
|
||||
t.Errorf("allowed %d restarts, want %d (max per window)", allowed, policy.maxRetries)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("counter resets after window elapses", func(t *testing.T) {
|
||||
state := retryState{attempts: 3, windowStart: base, lastRestart: base}
|
||||
ok, newState := decideRestart(state, policy, base.Add(policy.window+time.Second))
|
||||
if !ok {
|
||||
t.Error("expected restart allowed once the window elapsed")
|
||||
}
|
||||
if newState.attempts != 1 {
|
||||
t.Errorf("attempts = %d, want 1 after window reset", newState.attempts)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestPruneRetries(t *testing.T) {
|
||||
now := time.Date(2026, 6, 28, 12, 0, 0, 0, time.UTC)
|
||||
s := &Service{retries: map[string]retryState{
|
||||
// within the window -> retained
|
||||
"fresh": {attempts: 1, windowStart: now.Add(-time.Minute), lastRestart: now.Add(-time.Minute)},
|
||||
// exactly at the window boundary -> pruned
|
||||
"edge": {attempts: 2, windowStart: now.Add(-retryWindow), lastRestart: now.Add(-retryWindow)},
|
||||
// long past the window -> pruned
|
||||
"stale": {attempts: 3, windowStart: now.Add(-2 * retryWindow), lastRestart: now.Add(-2 * retryWindow)},
|
||||
}}
|
||||
|
||||
s.pruneRetries(now)
|
||||
|
||||
if _, ok := s.retries["fresh"]; !ok {
|
||||
t.Error("entry within the retry window should be retained")
|
||||
}
|
||||
if _, ok := s.retries["edge"]; ok {
|
||||
t.Error("entry exactly at the window boundary should be pruned")
|
||||
}
|
||||
if _, ok := s.retries["stale"]; ok {
|
||||
t.Error("entry past the retry window should be pruned")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRetryStateSurvivesStartingTick locks in the F1 fix: a container that flaps
|
||||
// through "starting" right after a restart (and so briefly drops out of the
|
||||
// health=unhealthy filter) must keep its retry accounting across the tick where
|
||||
// it is not observed, otherwise the cooldown / max-retries storm guard is
|
||||
// defeated and the next unhealthy observation triggers an immediate restart.
|
||||
func TestRetryStateSurvivesStartingTick(t *testing.T) {
|
||||
policy := retryPolicy{maxRetries: 3, window: retryWindow, cooldown: restartCooldown}
|
||||
const id = "flapper"
|
||||
s := &Service{retries: make(map[string]retryState)}
|
||||
|
||||
t0 := time.Date(2026, 6, 28, 12, 0, 0, 0, time.UTC)
|
||||
|
||||
// Tick 1: container is unhealthy -> first restart.
|
||||
ok, state := decideRestart(s.getRetry(id), policy, t0)
|
||||
s.setRetry(id, state)
|
||||
if !ok || state.attempts != 1 {
|
||||
t.Fatalf("tick 1: ok=%v attempts=%d, want restart with attempts=1", ok, state.attempts)
|
||||
}
|
||||
|
||||
// Tick 2 (t0+30s): the container is "starting" and not in the unhealthy list.
|
||||
// Prune must NOT drop its state because the window has not elapsed.
|
||||
s.pruneRetries(t0.Add(30 * time.Second))
|
||||
if _, kept := s.retries[id]; !kept {
|
||||
t.Fatal("tick 2: retry state was pruned while the container was 'starting'")
|
||||
}
|
||||
|
||||
// Tick 3 (t0+45s): unhealthy again, still within the cooldown. The surviving
|
||||
// state must block the restart and the attempt count must not be reset.
|
||||
ok, state = decideRestart(s.getRetry(id), policy, t0.Add(45*time.Second))
|
||||
s.setRetry(id, state)
|
||||
if ok {
|
||||
t.Error("tick 3: restart should be blocked by the surviving cooldown")
|
||||
}
|
||||
if state.attempts != 1 {
|
||||
t.Errorf("tick 3: attempts = %d, want 1 (state survived, not reset)", state.attempts)
|
||||
}
|
||||
}
|
||||
@@ -1,598 +0,0 @@
|
||||
package containerautomation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/docker/images"
|
||||
"github.com/portainer/portainer/api/internal/endpointutils"
|
||||
"github.com/portainer/portainer/api/stacks/deployments"
|
||||
"github.com/portainer/portainer/api/stacks/stackutils"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/image"
|
||||
dockerclient "github.com/docker/docker/client"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
const (
|
||||
// statusCheckTimeout bounds a single container image-status resolution
|
||||
// (container inspect + remote digest fetch).
|
||||
statusCheckTimeout = 30 * time.Second
|
||||
// recreateTimeout bounds a standalone recreate (pull + stop + create + start).
|
||||
// Pulls can be slow, so it is generous.
|
||||
recreateTimeout = 10 * time.Minute
|
||||
// stackRedeployTimeout bounds a single stack redeploy-with-pull.
|
||||
stackRedeployTimeout = 15 * time.Minute
|
||||
)
|
||||
|
||||
// update runs a single auto-update pass over every reachable Docker endpoint.
|
||||
// It is registered with the scheduler and guarded against overlapping ticks by
|
||||
// the Service. Errors are logged per endpoint/container so one failure does not
|
||||
// abort the whole pass; it always returns nil so the scheduler keeps the job.
|
||||
func (s *Service) update() error {
|
||||
if !s.updateRunning.CompareAndSwap(false, true) {
|
||||
log.Debug().Msg("auto-update: previous run still in progress, skipping tick")
|
||||
return nil
|
||||
}
|
||||
defer s.updateRunning.Store(false)
|
||||
|
||||
settings, err := s.dataStore.Settings().Settings()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("auto-update: unable to read settings")
|
||||
return nil
|
||||
}
|
||||
|
||||
scope := ScopeLabeled
|
||||
if settings.ContainerAutomation.AutoUpdate.Scope == ScopeAll {
|
||||
scope = ScopeAll
|
||||
}
|
||||
|
||||
opts := updateOptions{
|
||||
cleanup: settings.ContainerAutomation.AutoUpdate.Cleanup,
|
||||
rollback: settings.ContainerAutomation.AutoUpdate.RollbackOnFailure,
|
||||
rollbackTimeout: parseRollbackTimeout(settings.ContainerAutomation.AutoUpdate.RollbackTimeout),
|
||||
}
|
||||
|
||||
endpoints, err := s.dataStore.Endpoint().Endpoints()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("auto-update: unable to list environments")
|
||||
return nil
|
||||
}
|
||||
|
||||
for i := range endpoints {
|
||||
endpoint := &endpoints[i]
|
||||
|
||||
// Native Docker endpoints only: Kubernetes is not applicable and
|
||||
// Edge/async endpoints are not reachable synchronously from the scheduler.
|
||||
if !endpointutils.IsDockerEndpoint(endpoint) || endpointutils.IsEdgeEndpoint(endpoint) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Per-endpoint opt-out (M5): skip environments where automation is disabled,
|
||||
// independently of the global switch. Zero value participates, so existing
|
||||
// installs are unaffected.
|
||||
if !AutomationEnabledForEndpoint(endpoint) {
|
||||
log.Debug().Int("endpoint_id", int(endpoint.ID)).
|
||||
Msg("auto-update: automation disabled for this environment, skipping")
|
||||
continue
|
||||
}
|
||||
|
||||
s.updateEndpoint(endpoint, scope, opts)
|
||||
}
|
||||
|
||||
// Drop rolled-back records whose cooldown has fully elapsed (mirrors auto-heal's
|
||||
// pruneRetries), so the loop-guard map cannot grow unbounded.
|
||||
s.pruneRolledBack(time.Now())
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// updateOptions carries the per-pass auto-update toggles resolved from settings.
|
||||
type updateOptions struct {
|
||||
// cleanup removes the now-dangling old image after a confirmed-good update.
|
||||
cleanup bool
|
||||
// rollback enables the health gate + rollback of a failed standalone update.
|
||||
rollback bool
|
||||
// rollbackTimeout bounds how long the health gate waits before rolling back.
|
||||
rollbackTimeout time.Duration
|
||||
}
|
||||
|
||||
// parseRollbackTimeout resolves the configured rollback timeout, falling back to
|
||||
// the default when empty or unparseable.
|
||||
func parseRollbackTimeout(raw string) time.Duration {
|
||||
d, err := time.ParseDuration(raw)
|
||||
if err != nil || d <= 0 {
|
||||
return defaultRollbackTimeout
|
||||
}
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
// updateEndpoint applies image updates to the in-scope, outdated containers of a
|
||||
// single endpoint, routing each container to the standalone / stack / external
|
||||
// apply path. Stack-managed candidates are grouped so each owning stack is
|
||||
// redeployed at most once per tick.
|
||||
func (s *Service) updateEndpoint(endpoint *portainer.Endpoint, scope string, opts updateOptions) {
|
||||
endpointID := int(endpoint.ID)
|
||||
|
||||
// Swarm note (M4 limitation, mirrors auto-heal): we connect to the endpoint's
|
||||
// primary node only (nodeName ""). Containers scheduled on other Swarm nodes
|
||||
// are not updated here; stacks are redeployed cluster-wide by the swarm engine.
|
||||
clientTimeout := endpointTimeout
|
||||
cli, err := s.clientFactory.CreateClient(endpoint, "", &clientTimeout)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Int("endpoint_id", endpointID).Msg("auto-update: unable to create Docker client")
|
||||
return
|
||||
}
|
||||
defer cli.Close()
|
||||
|
||||
listCtx, cancel := context.WithTimeout(s.baseCtx, endpointTimeout)
|
||||
defer cancel()
|
||||
|
||||
// Running containers only: a stopped container has nothing to update now and
|
||||
// would be started by a bare recreate.
|
||||
containers, err := cli.ContainerList(listCtx, container.ListOptions{All: false})
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Int("endpoint_id", endpointID).Msg("auto-update: unable to list containers")
|
||||
return
|
||||
}
|
||||
|
||||
// Collect the in-scope, outdated, non-monitor-only containers as candidates.
|
||||
// An in-scope monitor-only container is still status-checked (keeping its badge
|
||||
// cache warm) but never auto-applied. This only covers in-scope containers: in
|
||||
// "labeled" scope a monitor-only container without the enable label is filtered
|
||||
// out below before any status check, so its badge is not refreshed here.
|
||||
var candidates []UpdateCandidate
|
||||
for _, c := range containers {
|
||||
if !InUpdateScope(scope, c.Labels) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Resolve the image status. This also refreshes the package-level status
|
||||
// cache that backs the badge, so in-scope monitor-only containers are still
|
||||
// checked even though they are never auto-applied.
|
||||
statusCtx, statusCancel := context.WithTimeout(s.baseCtx, statusCheckTimeout)
|
||||
status, err := s.digestClient.ContainerImageStatus(statusCtx, c.ID, endpoint, "")
|
||||
statusCancel()
|
||||
if err != nil {
|
||||
// Pull / registry-auth / network failure: leave the running container
|
||||
// untouched, never recreate on a failed check.
|
||||
log.Warn().Err(err).Str("container_id", c.ID).Int("endpoint_id", endpointID).
|
||||
Msg("auto-update: image status check failed, leaving container untouched")
|
||||
continue
|
||||
}
|
||||
|
||||
if status != images.Outdated {
|
||||
continue
|
||||
}
|
||||
|
||||
// Monitor-only: detect-only, never auto-apply (status already cached above).
|
||||
if IsMonitorOnly(c.Labels) {
|
||||
log.Info().Str("container_id", c.ID).Int("endpoint_id", endpointID).
|
||||
Msg("auto-update: outdated image detected but container is monitor-only, not applying")
|
||||
continue
|
||||
}
|
||||
|
||||
candidates = append(candidates, UpdateCandidate{ID: c.ID, Name: containerName(c.Names), ImageID: c.ImageID, Image: c.Image, Labels: c.Labels})
|
||||
}
|
||||
|
||||
// Route and de-duplicate: one redeploy per stack per tick.
|
||||
grouped := groupContainersForUpdate(candidates, s.stackLookupForEndpoint(endpoint.ID))
|
||||
|
||||
for _, ext := range grouped.External {
|
||||
log.Debug().Str("container_id", ext.ID).Int("endpoint_id", endpointID).
|
||||
Msg("auto-update: outdated externally-managed compose container, detect only")
|
||||
}
|
||||
|
||||
for _, c := range grouped.Standalone {
|
||||
s.updateStandalone(cli, endpoint, c, opts)
|
||||
}
|
||||
|
||||
for _, st := range grouped.Stacks {
|
||||
s.updateStack(cli, endpoint, st)
|
||||
}
|
||||
}
|
||||
|
||||
// stackLookupForEndpoint builds a compose-project-name -> Portainer compose stack
|
||||
// resolver for a single endpoint. Only Docker Compose stacks on this endpoint
|
||||
// match; a same-named swarm/kubernetes stack is treated as external (mirrors
|
||||
// M3's resolveContainerUpdatePath).
|
||||
func (s *Service) stackLookupForEndpoint(endpointID portainer.EndpointID) func(project string) *StackMatch {
|
||||
stacks, err := s.dataStore.Stack().ReadAll()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Int("endpoint_id", int(endpointID)).
|
||||
Msg("auto-update: unable to read stacks, treating compose containers as external")
|
||||
return func(string) *StackMatch { return nil }
|
||||
}
|
||||
|
||||
byName := make(map[string]*StackMatch)
|
||||
for i := range stacks {
|
||||
st := &stacks[i]
|
||||
if st.EndpointID != endpointID || st.Type != portainer.DockerComposeStack {
|
||||
continue
|
||||
}
|
||||
|
||||
byName[st.Name] = &StackMatch{StackID: int(st.ID), IsGit: st.WorkflowID != 0}
|
||||
}
|
||||
|
||||
return func(project string) *StackMatch {
|
||||
return byName[project]
|
||||
}
|
||||
}
|
||||
|
||||
// updateStandalone recreates a standalone container with a re-pull of its image,
|
||||
// then (when rollback is enabled and the container has a healthcheck) holds a
|
||||
// health gate over the new container and rolls back to the previous image if it
|
||||
// fails to become healthy. The old-image cleanup is deliberately ordered AFTER
|
||||
// the health gate, so the rollback target is never removed before the update is
|
||||
// confirmed good.
|
||||
//
|
||||
// Sequence: capture old image id + original ref + healthcheck -> recreate(pull)
|
||||
// -> [health gate] -> on healthy: cleanup (if enabled); on unhealthy: rollback
|
||||
// (never cleanup).
|
||||
func (s *Service) updateStandalone(cli *dockerclient.Client, endpoint *portainer.Endpoint, c UpdateCandidate, opts updateOptions) {
|
||||
endpointID := int(endpoint.ID)
|
||||
|
||||
// Loop-guard safety: the rolled-back map is keyed by endpoint+name (the only
|
||||
// identifier that survives a recreate). An unnamed container cannot be recorded
|
||||
// (recordRolledBack skips it), so with rollback enabled a container that keeps
|
||||
// failing its health gate would update->rollback every tick with NO suppression.
|
||||
// Skip the unnamed case when rollback is on so it cannot enter that
|
||||
// unsuppressable loop; detection/badge refresh already happened upstream and is
|
||||
// unaffected. (With rollback off there is no rollback to loop, so we proceed.)
|
||||
if skipUnnamedForRollback(opts.rollback, c.Name) {
|
||||
log.Info().Str("container_id", c.ID).Int("endpoint_id", endpointID).
|
||||
Msg("auto-update: skipping unnamed standalone container, rollback is enabled but there is no stable name to key the loop guard")
|
||||
return
|
||||
}
|
||||
|
||||
// Update->rollback loop guard: if this container's update was rolled back
|
||||
// recently and the remote still points at the SAME failed image, skip it until
|
||||
// the cooldown elapses. A genuinely new upstream image (a changed remote digest)
|
||||
// is not blocked.
|
||||
rollbackMapKey := rollbackKey(endpoint.ID, c.Name)
|
||||
if rec, ok := s.getRolledBack(rollbackMapKey); ok && s.shouldSkipRolledBack(rollbackMapKey, rec) {
|
||||
log.Info().Str("container_id", c.ID).Str("container", c.Name).Str("image", rec.ref).Int("endpoint_id", endpointID).
|
||||
Msg("auto-update: skipping update, a recent rollback failed on this image and the remote is unchanged (cooldown)")
|
||||
return
|
||||
}
|
||||
|
||||
// Capture the pre-update image identity for a possible rollback. The container
|
||||
// list gives us the old image id; an inspect adds the original reference (re-tag
|
||||
// target), whether a usable healthcheck exists, and the healthcheck start_period
|
||||
// (which must be waited out before deciding). We only health-gate when rollback
|
||||
// is enabled, the container has a healthcheck, we resolved both the old image id
|
||||
// and its reference, and that reference is a proper tag (a digest-pinned or bare
|
||||
// image id cannot be re-tagged, so the gate could never roll back).
|
||||
oldImageID := c.ImageID
|
||||
var originalRef string
|
||||
var startPeriod time.Duration
|
||||
healthGated := false
|
||||
if opts.rollback {
|
||||
// Bound the inspect like every other engine call so a hung/unreachable engine
|
||||
// cannot block the whole sequential tick until shutdown.
|
||||
inspectCtx, inspectCancel := context.WithTimeout(s.baseCtx, endpointTimeout)
|
||||
inspect, err := cli.ContainerInspect(inspectCtx, c.ID)
|
||||
inspectCancel()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Str("container_id", c.ID).Int("endpoint_id", endpointID).
|
||||
Msg("auto-update: unable to inspect container before update, proceeding without a health gate")
|
||||
} else {
|
||||
originalRef = inspect.Config.Image
|
||||
if oldImageID == "" {
|
||||
oldImageID = inspect.Image
|
||||
}
|
||||
if hc := inspect.Config.Healthcheck; hc != nil {
|
||||
startPeriod = hc.StartPeriod
|
||||
}
|
||||
|
||||
switch {
|
||||
case !hasHealthGate(inspect.Config.Healthcheck):
|
||||
log.Info().Str("container_id", c.ID).Int("endpoint_id", endpointID).
|
||||
Msg("auto-update: container has no healthcheck, updating without a rollback gate")
|
||||
case oldImageID == "" || originalRef == "":
|
||||
log.Info().Str("container_id", c.ID).Int("endpoint_id", endpointID).
|
||||
Msg("auto-update: unable to resolve previous image identity, updating without a rollback gate")
|
||||
case !isTagReference(originalRef):
|
||||
log.Info().Str("container_id", c.ID).Str("image", originalRef).Int("endpoint_id", endpointID).
|
||||
Msg("auto-update: health gate skipped, image is digest-pinned and cannot be rolled back")
|
||||
default:
|
||||
healthGated = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(s.baseCtx, recreateTimeout)
|
||||
defer cancel()
|
||||
|
||||
newContainer, err := s.containerService.Recreate(ctx, endpoint, c.ID, true, "", "")
|
||||
if err != nil {
|
||||
// Recreate preserves config and rolls back on a create failure; a pull or
|
||||
// create failure leaves the original container running.
|
||||
log.Warn().Err(err).Str("container_id", c.ID).Int("endpoint_id", endpointID).
|
||||
Msg("auto-update: failed to recreate standalone container")
|
||||
s.notifier.Notify(Event{
|
||||
Kind: EventUpdateFailed, EndpointID: endpointID, ContainerID: c.ID, ContainerName: c.Name,
|
||||
Message: "failed to recreate standalone container", Err: err,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
log.Info().Str("container_id", c.ID).Int("endpoint_id", endpointID).
|
||||
Msg("auto-update: recreated standalone container with updated image")
|
||||
newImage := ""
|
||||
if newContainer != nil {
|
||||
newImage = newContainer.Config.Image
|
||||
}
|
||||
|
||||
// Health gate: roll back if the new container does not become healthy in time.
|
||||
// The old image is preserved (not cleaned up) until the gate confirms health,
|
||||
// so the rollback target is still available. The "updated" event is held until
|
||||
// the gate confirms health, so an observer never sees a misleading
|
||||
// "updated" -> "rollback" sequence for the same container; on the rollback path
|
||||
// only EventRollback (or update-failed) is emitted.
|
||||
if healthGated {
|
||||
switch s.healthGate(cli, newContainer.ID, opts.rollbackTimeout, startPeriod) {
|
||||
case gateAborted:
|
||||
// Server shutdown mid-gate: leave the new container in place, do not roll
|
||||
// back and do not emit an event (we never observed a real failure).
|
||||
return
|
||||
case gateRollback:
|
||||
s.rollback(cli, endpoint, newContainer.ID, oldImageID, originalRef, c.Name)
|
||||
return
|
||||
case gateHealthy:
|
||||
// Confirmed healthy: fall through to emit "updated" and clean up.
|
||||
}
|
||||
}
|
||||
|
||||
// Emit "updated" now: either there was no gate (emitted right after recreate,
|
||||
// as before), or the gate confirmed the new container is healthy.
|
||||
s.notifier.Notify(Event{
|
||||
Kind: EventUpdated, EndpointID: endpointID, ContainerID: newContainer.ID, ContainerName: c.Name,
|
||||
Image: newImage, OldDigest: oldImageID, NewDigest: newContainer.Image,
|
||||
Message: "updated standalone container",
|
||||
})
|
||||
|
||||
if opts.cleanup && newContainer != nil && newContainer.Image != oldImageID {
|
||||
s.cleanupOldImage(cli, endpoint, oldImageID)
|
||||
}
|
||||
}
|
||||
|
||||
// containerName returns a container's primary name without the leading slash, or
|
||||
// "" when none is reported. The name is stable across a recreate (Recreate
|
||||
// assigns a new container ID but preserves the name), so it keys the rolled-back
|
||||
// loop-guard map.
|
||||
func containerName(names []string) string {
|
||||
if len(names) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
return strings.TrimPrefix(names[0], "/")
|
||||
}
|
||||
|
||||
// skipUnnamedForRollback reports whether a standalone update must be skipped
|
||||
// because rollback is enabled but the container has no stable name to key the
|
||||
// loop guard. The rolled-back map is keyed by endpoint+name (the only identifier
|
||||
// that survives a recreate); without a name the guard cannot record a failed
|
||||
// target, so a repeatedly-failing update would loop update->rollback every tick
|
||||
// with no suppression. When rollback is off there is nothing to loop, so an
|
||||
// unnamed container is still allowed to update.
|
||||
func skipUnnamedForRollback(rollback bool, name string) bool {
|
||||
return rollback && name == ""
|
||||
}
|
||||
|
||||
// rollbackKey identifies a standalone container in the rolled-back map by its
|
||||
// endpoint and (recreate-stable) name. A recreate assigns a new container ID, so
|
||||
// the ID cannot key state across an update; the name is preserved.
|
||||
func rollbackKey(endpointID portainer.EndpointID, name string) string {
|
||||
return fmt.Sprintf("%d/%s", int(endpointID), name)
|
||||
}
|
||||
|
||||
// resolveRemoteDigest fetches the current remote image digest for a reference. It
|
||||
// tells whether a rolled-back container's upstream target is still the same
|
||||
// failed image (skip) or a new push (retry).
|
||||
func (s *Service) resolveRemoteDigest(ctx context.Context, ref string) (string, error) {
|
||||
img, err := images.ParseImage(images.ParseImageOptions{Name: ref})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
dig, err := s.digestClient.RemoteDigest(ctx, img)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return dig.String(), nil
|
||||
}
|
||||
|
||||
// recordRolledBack stores the failed target after a successful rollback so the
|
||||
// next poll skips re-pulling the same broken image. The failed remote digest is
|
||||
// resolved now (the registry is reachable, the image was just pulled); if it
|
||||
// cannot be resolved the record is still stored with an empty digest and the
|
||||
// guard skips conservatively until the cooldown elapses.
|
||||
func (s *Service) recordRolledBack(endpoint *portainer.Endpoint, name, ref string) {
|
||||
if name == "" {
|
||||
// Without a stable key we cannot reliably match the container next tick.
|
||||
log.Debug().Str("image", ref).Int("endpoint_id", int(endpoint.ID)).
|
||||
Msg("auto-update: rolled-back container has no name, loop guard not recorded")
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(s.baseCtx, statusCheckTimeout)
|
||||
digest, err := s.resolveRemoteDigest(ctx, ref)
|
||||
cancel()
|
||||
if err != nil {
|
||||
log.Debug().Err(err).Str("image", ref).Int("endpoint_id", int(endpoint.ID)).
|
||||
Msg("auto-update: could not resolve failed remote digest, loop guard will skip conservatively until cooldown")
|
||||
}
|
||||
|
||||
s.setRolledBack(rollbackKey(endpoint.ID, name), rolledBackTarget{ref: ref, digest: digest, at: time.Now()})
|
||||
}
|
||||
|
||||
// shouldSkipRolledBack reports whether a standalone container must be skipped this
|
||||
// tick to avoid the update->rollback loop, clearing the record once the skip no
|
||||
// longer applies (cooldown elapsed or a new upstream image). It resolves the
|
||||
// current remote digest so a genuinely new image is never blocked.
|
||||
func (s *Service) shouldSkipRolledBack(key string, rec rolledBackTarget) bool {
|
||||
now := time.Now()
|
||||
|
||||
// Fast paths that avoid a registry call: cooldown elapsed -> clear & proceed;
|
||||
// no recorded digest -> skip conservatively while the cooldown is open.
|
||||
if now.Sub(rec.at) >= updateRollbackCooldown {
|
||||
s.clearRolledBack(key)
|
||||
return false
|
||||
}
|
||||
if rec.digest == "" {
|
||||
return true
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(s.baseCtx, statusCheckTimeout)
|
||||
currentDigest, err := s.resolveRemoteDigest(ctx, rec.ref)
|
||||
cancel()
|
||||
if err != nil {
|
||||
// Cannot confirm the upstream target changed: stay conservative and skip to
|
||||
// avoid re-entering the loop, until the cooldown elapses.
|
||||
log.Debug().Err(err).Str("image", rec.ref).
|
||||
Msg("auto-update: cannot resolve remote digest for a rolled-back container, skipping until cooldown")
|
||||
return true
|
||||
}
|
||||
|
||||
if decideUpdateSkip(rec, currentDigest, now, updateRollbackCooldown) {
|
||||
return true
|
||||
}
|
||||
|
||||
// New upstream image (changed digest): the failed target is gone, clear the
|
||||
// record and let the update proceed.
|
||||
s.clearRolledBack(key)
|
||||
return false
|
||||
}
|
||||
|
||||
// cleanupOldImage attempts a conservative removal of the previous image after a
|
||||
// standalone update. The removal is NOT forced: Docker refuses to delete an
|
||||
// image that still carries tags or is referenced by any container, so this only
|
||||
// succeeds when the old image has become genuinely dangling (untagged and
|
||||
// unused). It never touches a tagged image still in use.
|
||||
func (s *Service) cleanupOldImage(cli *dockerclient.Client, endpoint *portainer.Endpoint, oldImageID string) {
|
||||
if oldImageID == "" {
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(s.baseCtx, endpointTimeout)
|
||||
defer cancel()
|
||||
|
||||
if _, err := cli.ImageRemove(ctx, oldImageID, image.RemoveOptions{Force: false, PruneChildren: false}); err != nil {
|
||||
log.Debug().Err(err).Str("image_id", oldImageID).Int("endpoint_id", int(endpoint.ID)).
|
||||
Msg("auto-update: old image not removed (still tagged or in use)")
|
||||
return
|
||||
}
|
||||
|
||||
log.Info().Str("image_id", oldImageID).Int("endpoint_id", int(endpoint.ID)).
|
||||
Msg("auto-update: removed dangling old image after update")
|
||||
}
|
||||
|
||||
// updateStack applies an image update to a Portainer-managed compose stack so its
|
||||
// containers are recreated by the stack engine and stay part of the stack. It is
|
||||
// called at most once per stack per tick.
|
||||
//
|
||||
// - git stacks: detect-only here. A git stack's source of truth is its commit;
|
||||
// this tick's trigger is an image-only update (same compose manifest, newer
|
||||
// upstream digest), which the git redeploy path (RedeployWhenChanged) would
|
||||
// short-circuit without applying — while still doing a real git fetch every
|
||||
// tick. So we skip git stacks: the image update lands on the stack's next git
|
||||
// change or via a manual "Update now", and we do not fetch git every tick.
|
||||
// - file stacks: the deployer is driven directly with forcePullImage=true,
|
||||
// applying the image update immediately.
|
||||
//
|
||||
// On a successful file-stack redeploy it emits one EventUpdated per member
|
||||
// container that triggered the update (not a single aggregate stack event), each
|
||||
// carrying the stack name and a best-effort post-redeploy new image id.
|
||||
func (s *Service) updateStack(cli *dockerclient.Client, endpoint *portainer.Endpoint, st StackUpdate) {
|
||||
if st.IsGit {
|
||||
// Detect-only: leave git bookkeeping to the git redeploy path. Logged at
|
||||
// debug so it does not repeat at info on every tick (it would otherwise
|
||||
// fire for an unchanged git stack indefinitely).
|
||||
log.Debug().Int("stack_id", st.StackID).Int("endpoint_id", int(endpoint.ID)).
|
||||
Msg("auto-update: outdated git stack image detected, detect only (applied on next git change or manual update)")
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(s.baseCtx, stackRedeployTimeout)
|
||||
defer cancel()
|
||||
|
||||
stack, err := s.dataStore.Stack().Read(portainer.StackID(st.StackID))
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Int("stack_id", st.StackID).Int("endpoint_id", int(endpoint.ID)).
|
||||
Msg("auto-update: unable to read stack for redeploy")
|
||||
return
|
||||
}
|
||||
|
||||
// Resolve registries the same way the established userless/system redeploy does
|
||||
// (RedeployWhenChanged): scope them to the stack author's access on the endpoint
|
||||
// and refresh ECR tokens, so an ECR-backed stack authenticates with fresh
|
||||
// credentials instead of the stale token a raw ReadAll() would pass.
|
||||
registries, err := deployments.ResolveStackRegistries(s.dataStore, stack, endpoint.ID)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Int("stack_id", st.StackID).Int("endpoint_id", int(endpoint.ID)).
|
||||
Msg("auto-update: unable to resolve registries for stack redeploy")
|
||||
return
|
||||
}
|
||||
|
||||
// prune=false (conservative: do not remove resources the user may rely on),
|
||||
// forcePullImage=true (the whole point), forceRecreate=false.
|
||||
if stackutils.IsRelativePathStack(stack) {
|
||||
err = s.stackDeployer.DeployRemoteComposeStack(ctx, stack, endpoint, registries, false, true, false)
|
||||
} else {
|
||||
err = s.stackDeployer.DeployComposeStack(ctx, stack, endpoint, registries, false, true, false)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Int("stack_id", st.StackID).Int("endpoint_id", int(endpoint.ID)).
|
||||
Msg("auto-update: failed to redeploy compose stack with re-pull")
|
||||
return
|
||||
}
|
||||
|
||||
log.Info().Int("stack_id", st.StackID).Int("endpoint_id", int(endpoint.ID)).
|
||||
Msg("auto-update: redeployed compose stack with updated images")
|
||||
|
||||
// One notification PER updated container (the maintainer's requirement), each
|
||||
// showing the container's stack name. The stack was redeployed as a whole, so the
|
||||
// per-container new image id is not in hand; re-inspect each container by its
|
||||
// (compose-stable) name to fill in the "new" digest best-effort. A failed inspect
|
||||
// leaves NewDigest empty and the message falls back to "image updated" — never a
|
||||
// blocked delivery.
|
||||
for _, c := range st.Containers {
|
||||
s.notifier.Notify(Event{
|
||||
Kind: EventUpdated, EndpointID: int(endpoint.ID), StackID: st.StackID,
|
||||
StackName: c.Labels[composeProjectLabel], ContainerName: c.Name,
|
||||
Image: c.Image, OldDigest: c.ImageID, NewDigest: s.inspectImageID(cli, c.Name),
|
||||
Message: "updated stack container",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// inspectImageID re-inspects a container by its (compose-stable) name after a stack
|
||||
// redeploy to recover the new local image id for the update notification. It is
|
||||
// best-effort: any failure (or an empty name) yields "", and the caller degrades the
|
||||
// message to "image updated" rather than blocking delivery. The inspect is bounded
|
||||
// like every other engine call so a hung engine cannot stall the tick.
|
||||
func (s *Service) inspectImageID(cli *dockerclient.Client, containerName string) string {
|
||||
if containerName == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(s.baseCtx, endpointTimeout)
|
||||
defer cancel()
|
||||
|
||||
inspect, err := cli.ContainerInspect(ctx, containerName)
|
||||
if err != nil {
|
||||
log.Debug().Err(err).Str("container", containerName).
|
||||
Msg("auto-update: unable to inspect stack container for its new image id, notifying without it")
|
||||
return ""
|
||||
}
|
||||
|
||||
return inspect.Image
|
||||
}
|
||||
@@ -1,148 +0,0 @@
|
||||
package containerautomation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/datastore"
|
||||
"github.com/portainer/portainer/api/internal/testhelpers"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
dockerclient "github.com/docker/docker/client"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// newStackInspectClient builds a Docker client wired to a test server that answers
|
||||
// ContainerInspect by name, returning the given new image id. It is the seam the
|
||||
// post-redeploy best-effort "new digest" re-inspect uses.
|
||||
func newStackInspectClient(t *testing.T, newImageIDByName map[string]string) *dockerclient.Client {
|
||||
t.Helper()
|
||||
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
for name, imageID := range newImageIDByName {
|
||||
if strings.HasSuffix(r.URL.Path, "/containers/"+name+"/json") {
|
||||
_ = json.NewEncoder(w).Encode(container.InspectResponse{
|
||||
ContainerJSONBase: &container.ContainerJSONBase{ID: name, Image: imageID},
|
||||
Config: &container.Config{},
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
http.Error(w, "not found", http.StatusNotFound)
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
|
||||
cli, err := dockerclient.NewClientWithOpts(
|
||||
dockerclient.WithHost(srv.URL),
|
||||
dockerclient.WithHTTPClient(http.DefaultClient),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
return cli
|
||||
}
|
||||
|
||||
// TestUpdateStackEmitsPerContainerEvents proves the maintainer's requirement: a
|
||||
// (file) stack redeploy emits one EventUpdated PER updated member container, each
|
||||
// carrying the compose stack name (from the container's label, not a Stack().Read)
|
||||
// and a best-effort post-redeploy new image id — never a single aggregate stack
|
||||
// event.
|
||||
func TestUpdateStackEmitsPerContainerEvents(t *testing.T) {
|
||||
_, store := datastore.MustNewTestStore(t, true, false)
|
||||
|
||||
// A stack author must exist for registry resolution; an admin resolves to the
|
||||
// (empty) registry set without needing endpoint/team wiring.
|
||||
require.NoError(t, store.User().Create(&portainer.User{ID: 1, Username: "auto", Role: portainer.AdministratorRole}))
|
||||
|
||||
endpoint := &portainer.Endpoint{ID: 1, Name: "nebula.lc"}
|
||||
require.NoError(t, store.Endpoint().Create(endpoint))
|
||||
|
||||
require.NoError(t, store.Stack().Create(&portainer.Stack{
|
||||
ID: 7, EndpointID: 1, Name: "cache-demo", Type: portainer.DockerComposeStack, CreatedBy: "auto",
|
||||
}))
|
||||
|
||||
const (
|
||||
oldEsphome = "sha256:59b94983c73a000000000000000000000000000000000000000000000000aaaa"
|
||||
newEsphome = "sha256:2231ca5d676d000000000000000000000000000000000000000000000000bbbb"
|
||||
oldOther = "sha256:1111111111110000000000000000000000000000000000000000000000000000"
|
||||
newOther = "sha256:2222222222220000000000000000000000000000000000000000000000000000"
|
||||
)
|
||||
|
||||
cli := newStackInspectClient(t, map[string]string{
|
||||
"esphome": newEsphome,
|
||||
"other": newOther,
|
||||
})
|
||||
|
||||
rec := &recordingNotifier{}
|
||||
s := &Service{
|
||||
baseCtx: context.Background(),
|
||||
dataStore: store,
|
||||
stackDeployer: testhelpers.NewTestStackDeployer(),
|
||||
notifier: rec,
|
||||
}
|
||||
|
||||
st := StackUpdate{
|
||||
StackID: 7,
|
||||
IsGit: false,
|
||||
Containers: []UpdateCandidate{
|
||||
{Name: "esphome", ImageID: oldEsphome, Image: "esphome/esphome:latest", Labels: map[string]string{composeProjectLabel: "cache-demo"}},
|
||||
{Name: "other", ImageID: oldOther, Image: "redis:7", Labels: map[string]string{composeProjectLabel: "cache-demo"}},
|
||||
},
|
||||
}
|
||||
|
||||
s.updateStack(cli, endpoint, st)
|
||||
|
||||
require.Len(t, rec.events, 2, "one EventUpdated per updated member container, not one aggregate stack event")
|
||||
|
||||
byContainer := map[string]Event{}
|
||||
for _, e := range rec.events {
|
||||
require.Equal(t, EventUpdated, e.Kind)
|
||||
require.Equal(t, "cache-demo", e.StackName, "each per-container event carries the compose stack name")
|
||||
require.Equal(t, 7, e.StackID)
|
||||
byContainer[e.ContainerName] = e
|
||||
}
|
||||
|
||||
esphome, ok := byContainer["esphome"]
|
||||
require.True(t, ok, "expected a per-container event for esphome")
|
||||
require.Equal(t, oldEsphome, esphome.OldDigest)
|
||||
require.Equal(t, newEsphome, esphome.NewDigest, "the new image id is recovered by re-inspecting the container after redeploy")
|
||||
|
||||
other, ok := byContainer["other"]
|
||||
require.True(t, ok, "expected a per-container event for other")
|
||||
require.Equal(t, oldOther, other.OldDigest)
|
||||
require.Equal(t, newOther, other.NewDigest)
|
||||
}
|
||||
|
||||
// TestUpdateStackGitIsDetectOnly guards that a git stack stays detect-only: it is
|
||||
// not redeployed and emits no notification (its image update lands on the next git
|
||||
// change or a manual update).
|
||||
func TestUpdateStackGitIsDetectOnly(t *testing.T) {
|
||||
_, store := datastore.MustNewTestStore(t, true, false)
|
||||
|
||||
endpoint := &portainer.Endpoint{ID: 1, Name: "nebula.lc"}
|
||||
require.NoError(t, store.Endpoint().Create(endpoint))
|
||||
|
||||
deployer := testhelpers.NewTestStackDeployer()
|
||||
rec := &recordingNotifier{}
|
||||
s := &Service{
|
||||
baseCtx: context.Background(),
|
||||
dataStore: store,
|
||||
stackDeployer: deployer,
|
||||
notifier: rec,
|
||||
}
|
||||
|
||||
cli := newStackInspectClient(t, nil)
|
||||
|
||||
s.updateStack(cli, endpoint, StackUpdate{
|
||||
StackID: 9, IsGit: true,
|
||||
Containers: []UpdateCandidate{{Name: "esphome", Labels: map[string]string{composeProjectLabel: "cache-demo"}}},
|
||||
})
|
||||
|
||||
require.Empty(t, rec.events, "a git stack is detect-only, no per-container notification")
|
||||
require.Zero(t, deployer.DeployComposeCallCount, "a git stack must not be redeployed here")
|
||||
}
|
||||
@@ -1,257 +0,0 @@
|
||||
package containerautomation
|
||||
|
||||
import "strconv"
|
||||
|
||||
const (
|
||||
// Scope values shared by the auto-heal and auto-update global settings.
|
||||
ScopeLabeled = "labeled"
|
||||
ScopeAll = "all"
|
||||
|
||||
// Primary labels (with community aliases) controlling per-container auto-heal.
|
||||
labelEnable = "io.portainer.autoheal.enable"
|
||||
labelEnableAlias = "autoheal"
|
||||
labelStopTimeout = "io.portainer.autoheal.stop-timeout"
|
||||
labelStopTimeoutAlias = "autoheal.stop.timeout"
|
||||
labelRetries = "io.portainer.autoheal.retries"
|
||||
|
||||
// Primary labels (with watchtower aliases) controlling per-container auto-update.
|
||||
labelUpdateEnable = "io.portainer.update.enable"
|
||||
labelUpdateEnableAlias = "com.centurylinklabs.watchtower.enable"
|
||||
labelUpdateMonitorOnly = "io.portainer.update.monitor-only"
|
||||
labelUpdateMonitorOnlyAlias = "com.centurylinklabs.watchtower.monitor-only"
|
||||
|
||||
// composeProjectLabel identifies the compose project a container belongs to.
|
||||
composeProjectLabel = "com.docker.compose.project"
|
||||
|
||||
// Defaults used when a label is missing or holds an invalid value.
|
||||
defaultStopTimeout = 10
|
||||
defaultRetries = 3
|
||||
)
|
||||
|
||||
// InScope reports whether a container is subject to auto-heal given the global
|
||||
// scope and the container's labels.
|
||||
//
|
||||
// - "all": every container is in scope, unless it explicitly opts out with the
|
||||
// enable label set to false.
|
||||
// - "labeled" (default): only containers with the enable label set to true.
|
||||
func InScope(scope string, labels map[string]string) bool {
|
||||
enabled, present := boolLabel(labels, labelEnable, labelEnableAlias)
|
||||
|
||||
switch scope {
|
||||
case ScopeAll:
|
||||
if present && !enabled {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
default: // ScopeLabeled
|
||||
return present && enabled
|
||||
}
|
||||
}
|
||||
|
||||
// boolLabel resolves a boolean label (primary key first, alias second).
|
||||
// It returns the parsed value and whether the label was present at all.
|
||||
// Invalid values are treated as false but still count as "present".
|
||||
func boolLabel(labels map[string]string, key, alias string) (value bool, present bool) {
|
||||
raw, ok := labels[key]
|
||||
if !ok {
|
||||
raw, ok = labels[alias]
|
||||
}
|
||||
|
||||
if !ok {
|
||||
return false, false
|
||||
}
|
||||
|
||||
parsed, err := strconv.ParseBool(raw)
|
||||
if err != nil {
|
||||
return false, true
|
||||
}
|
||||
|
||||
return parsed, true
|
||||
}
|
||||
|
||||
// InUpdateScope reports whether a container is subject to auto-update given the
|
||||
// global scope and the container's labels. It mirrors InScope but reads the
|
||||
// update enable label (io.portainer.update.enable / watchtower alias):
|
||||
//
|
||||
// - "all": every container is in scope, unless it explicitly opts out with the
|
||||
// update enable label set to false.
|
||||
// - "labeled" (default): only containers with the update enable label true.
|
||||
func InUpdateScope(scope string, labels map[string]string) bool {
|
||||
enabled, present := boolLabel(labels, labelUpdateEnable, labelUpdateEnableAlias)
|
||||
|
||||
switch scope {
|
||||
case ScopeAll:
|
||||
if present && !enabled {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
default: // ScopeLabeled
|
||||
return present && enabled
|
||||
}
|
||||
}
|
||||
|
||||
// IsMonitorOnly reports whether a container is flagged detect-only via the
|
||||
// monitor-only label (io.portainer.update.monitor-only / watchtower alias).
|
||||
// Such containers have their image status resolved (for the badge cache) but are
|
||||
// never auto-applied.
|
||||
func IsMonitorOnly(labels map[string]string) bool {
|
||||
value, present := boolLabel(labels, labelUpdateMonitorOnly, labelUpdateMonitorOnlyAlias)
|
||||
|
||||
return present && value
|
||||
}
|
||||
|
||||
// UpdateKind is the apply path resolved for an outdated container.
|
||||
type UpdateKind string
|
||||
|
||||
const (
|
||||
// UpdateStandalone: recreate-with-pull (no compose project).
|
||||
UpdateStandalone UpdateKind = "standalone"
|
||||
// UpdateStack: redeploy the owning Portainer compose stack with re-pull, so
|
||||
// the container stays part of its stack.
|
||||
UpdateStack UpdateKind = "stack"
|
||||
// UpdateExternal: compose-managed but with no matching Portainer compose
|
||||
// stack record; Portainer must not touch it (would detach it / drift).
|
||||
UpdateExternal UpdateKind = "external"
|
||||
)
|
||||
|
||||
// StackMatch is the Portainer Docker Compose stack a compose project resolves to.
|
||||
type StackMatch struct {
|
||||
StackID int
|
||||
// IsGit routes file vs git redeploy at apply time.
|
||||
IsGit bool
|
||||
}
|
||||
|
||||
// UpdateRouting is the decision returned by resolveContainerUpdateRouting.
|
||||
type UpdateRouting struct {
|
||||
Kind UpdateKind
|
||||
StackID int
|
||||
IsGit bool
|
||||
}
|
||||
|
||||
// resolveContainerUpdateRouting decides how a container's image update must be
|
||||
// applied, given a lookup that resolves a compose project name to a matching
|
||||
// Portainer Docker Compose stack (nil when none exists or it is not a compose
|
||||
// stack). It is the Go equivalent of M3's TS resolveContainerUpdatePath: pure
|
||||
// and side-effect free so it can be unit-tested without Docker or the datastore.
|
||||
//
|
||||
// - No compose project label -> standalone (recreate-with-pull).
|
||||
// - Compose project matching a Portainer compose stack -> stack
|
||||
// (redeploy-with-pull, keeps the container in its stack).
|
||||
// - Compose project with no matching Portainer compose stack -> external
|
||||
// (managed outside Portainer / a same-named stack of another type), left
|
||||
// untouched to avoid detaching it or drifting.
|
||||
func resolveContainerUpdateRouting(labels map[string]string, stackLookup func(project string) *StackMatch) UpdateRouting {
|
||||
project := labels[composeProjectLabel]
|
||||
if project == "" {
|
||||
return UpdateRouting{Kind: UpdateStandalone}
|
||||
}
|
||||
|
||||
match := stackLookup(project)
|
||||
if match == nil {
|
||||
return UpdateRouting{Kind: UpdateExternal}
|
||||
}
|
||||
|
||||
return UpdateRouting{Kind: UpdateStack, StackID: match.StackID, IsGit: match.IsGit}
|
||||
}
|
||||
|
||||
// UpdateCandidate is an outdated, in-scope container considered for auto-update.
|
||||
type UpdateCandidate struct {
|
||||
ID string
|
||||
// Name is the container's primary name (no leading slash). It is stable across
|
||||
// a recreate and keys the update->rollback loop guard.
|
||||
Name string
|
||||
// ImageID is the pre-update local image id ("sha256:..."), the "old" digest in a
|
||||
// per-container update notification.
|
||||
ImageID string
|
||||
// Image is the container's image reference (e.g. "nginx:latest"), carried for the
|
||||
// notification.
|
||||
Image string
|
||||
Labels map[string]string
|
||||
}
|
||||
|
||||
// StackUpdate identifies a Portainer stack to redeploy once, together with the
|
||||
// affected member containers so each updated container can emit its own
|
||||
// notification (with the stack name) after the redeploy.
|
||||
type StackUpdate struct {
|
||||
StackID int
|
||||
IsGit bool
|
||||
// Containers are the outdated member containers that triggered this stack
|
||||
// redeploy, threaded through from detection so a per-container notification can
|
||||
// be emitted for each (name + old image id + image + labels/stack name).
|
||||
Containers []UpdateCandidate
|
||||
}
|
||||
|
||||
// GroupedUpdates partitions candidates into their apply paths, de-duplicating
|
||||
// stack containers so each owning stack is redeployed at most once per tick
|
||||
// (the overlap guard for stack fan-out). Pure and unit-testable, the Go analogue
|
||||
// of M3's groupContainersForUpdate.
|
||||
type GroupedUpdates struct {
|
||||
Standalone []UpdateCandidate
|
||||
External []UpdateCandidate
|
||||
Stacks []StackUpdate
|
||||
}
|
||||
|
||||
// groupContainersForUpdate routes each candidate and collapses stack candidates
|
||||
// so a stack with several outdated containers is redeployed only once.
|
||||
func groupContainersForUpdate(candidates []UpdateCandidate, stackLookup func(project string) *StackMatch) GroupedUpdates {
|
||||
grouped := GroupedUpdates{}
|
||||
// stackIndex maps a stack id to its slot in grouped.Stacks so a stack is
|
||||
// redeployed once, while every member container is still collected for its own
|
||||
// notification (rather than discarded at the collapse).
|
||||
stackIndex := make(map[int]int)
|
||||
|
||||
for _, c := range candidates {
|
||||
routing := resolveContainerUpdateRouting(c.Labels, stackLookup)
|
||||
switch routing.Kind {
|
||||
case UpdateStandalone:
|
||||
grouped.Standalone = append(grouped.Standalone, c)
|
||||
case UpdateExternal:
|
||||
grouped.External = append(grouped.External, c)
|
||||
case UpdateStack:
|
||||
idx, ok := stackIndex[routing.StackID]
|
||||
if !ok {
|
||||
grouped.Stacks = append(grouped.Stacks, StackUpdate{StackID: routing.StackID, IsGit: routing.IsGit})
|
||||
idx = len(grouped.Stacks) - 1
|
||||
stackIndex[routing.StackID] = idx
|
||||
}
|
||||
|
||||
grouped.Stacks[idx].Containers = append(grouped.Stacks[idx].Containers, c)
|
||||
}
|
||||
}
|
||||
|
||||
return grouped
|
||||
}
|
||||
|
||||
// StopTimeout returns the per-container stop timeout (in seconds) from labels,
|
||||
// falling back to the default when missing or invalid.
|
||||
func StopTimeout(labels map[string]string) int {
|
||||
return positiveIntLabel(labels, labelStopTimeout, labelStopTimeoutAlias, defaultStopTimeout)
|
||||
}
|
||||
|
||||
// MaxRetries returns the per-container max restarts per window from labels,
|
||||
// falling back to the default when missing or invalid.
|
||||
func MaxRetries(labels map[string]string) int {
|
||||
return positiveIntLabel(labels, labelRetries, "", defaultRetries)
|
||||
}
|
||||
|
||||
// positiveIntLabel reads an integer label (primary first, optional alias second)
|
||||
// and returns it when strictly positive, otherwise the provided default.
|
||||
func positiveIntLabel(labels map[string]string, key, alias string, fallback int) int {
|
||||
raw, ok := labels[key]
|
||||
if !ok && alias != "" {
|
||||
raw, ok = labels[alias]
|
||||
}
|
||||
|
||||
if !ok {
|
||||
return fallback
|
||||
}
|
||||
|
||||
value, err := strconv.Atoi(raw)
|
||||
if err != nil || value <= 0 {
|
||||
return fallback
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
@@ -1,248 +0,0 @@
|
||||
package containerautomation
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestInScope(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
scope string
|
||||
labels map[string]string
|
||||
want bool
|
||||
}{
|
||||
{"labeled: no labels", ScopeLabeled, nil, false},
|
||||
{"labeled: enable true (primary)", ScopeLabeled, map[string]string{labelEnable: "true"}, true},
|
||||
{"labeled: enable true (alias)", ScopeLabeled, map[string]string{labelEnableAlias: "true"}, true},
|
||||
{"labeled: enable false", ScopeLabeled, map[string]string{labelEnable: "false"}, false},
|
||||
{"labeled: enable bad value", ScopeLabeled, map[string]string{labelEnable: "yepp"}, false},
|
||||
{"labeled: primary wins over alias", ScopeLabeled, map[string]string{labelEnable: "true", labelEnableAlias: "false"}, true},
|
||||
{"all: no labels", ScopeAll, nil, true},
|
||||
{"all: enable true", ScopeAll, map[string]string{labelEnable: "true"}, true},
|
||||
{"all: explicit opt-out", ScopeAll, map[string]string{labelEnable: "false"}, false},
|
||||
{"all: opt-out via alias", ScopeAll, map[string]string{labelEnableAlias: "0"}, false},
|
||||
{"all: bad value is not opt-out", ScopeAll, map[string]string{labelEnable: "nope"}, false},
|
||||
{"unknown scope falls back to labeled", "weird", map[string]string{labelEnable: "true"}, true},
|
||||
{"unknown scope, no label", "weird", nil, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := InScope(tt.scope, tt.labels); got != tt.want {
|
||||
t.Errorf("InScope(%q, %v) = %v, want %v", tt.scope, tt.labels, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestStopTimeout(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
labels map[string]string
|
||||
want int
|
||||
}{
|
||||
{"default when missing", nil, defaultStopTimeout},
|
||||
{"primary value", map[string]string{labelStopTimeout: "25"}, 25},
|
||||
{"alias value", map[string]string{labelStopTimeoutAlias: "15"}, 15},
|
||||
{"primary wins over alias", map[string]string{labelStopTimeout: "25", labelStopTimeoutAlias: "15"}, 25},
|
||||
{"bad value falls back", map[string]string{labelStopTimeout: "abc"}, defaultStopTimeout},
|
||||
{"zero falls back", map[string]string{labelStopTimeout: "0"}, defaultStopTimeout},
|
||||
{"negative falls back", map[string]string{labelStopTimeout: "-5"}, defaultStopTimeout},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := StopTimeout(tt.labels); got != tt.want {
|
||||
t.Errorf("StopTimeout(%v) = %d, want %d", tt.labels, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaxRetries(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
labels map[string]string
|
||||
want int
|
||||
}{
|
||||
{"default when missing", nil, defaultRetries},
|
||||
{"explicit value", map[string]string{labelRetries: "5"}, 5},
|
||||
{"bad value falls back", map[string]string{labelRetries: "lots"}, defaultRetries},
|
||||
{"zero falls back", map[string]string{labelRetries: "0"}, defaultRetries},
|
||||
{"no alias for retries", map[string]string{"autoheal.retries": "7"}, defaultRetries},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := MaxRetries(tt.labels); got != tt.want {
|
||||
t.Errorf("MaxRetries(%v) = %d, want %d", tt.labels, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInUpdateScope(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
scope string
|
||||
labels map[string]string
|
||||
want bool
|
||||
}{
|
||||
{"labeled: no labels", ScopeLabeled, nil, false},
|
||||
{"labeled: enable true (primary)", ScopeLabeled, map[string]string{labelUpdateEnable: "true"}, true},
|
||||
{"labeled: enable true (watchtower alias)", ScopeLabeled, map[string]string{labelUpdateEnableAlias: "true"}, true},
|
||||
{"labeled: enable false", ScopeLabeled, map[string]string{labelUpdateEnable: "false"}, false},
|
||||
{"labeled: enable bad value", ScopeLabeled, map[string]string{labelUpdateEnable: "soon"}, false},
|
||||
{"labeled: primary wins over alias", ScopeLabeled, map[string]string{labelUpdateEnable: "true", labelUpdateEnableAlias: "false"}, true},
|
||||
{"all: no labels", ScopeAll, nil, true},
|
||||
{"all: enable true", ScopeAll, map[string]string{labelUpdateEnable: "true"}, true},
|
||||
{"all: explicit opt-out", ScopeAll, map[string]string{labelUpdateEnable: "false"}, false},
|
||||
{"all: opt-out via watchtower alias", ScopeAll, map[string]string{labelUpdateEnableAlias: "0"}, false},
|
||||
{"all: bad value is not opt-out", ScopeAll, map[string]string{labelUpdateEnable: "nope"}, false},
|
||||
{"unknown scope falls back to labeled", "weird", map[string]string{labelUpdateEnable: "true"}, true},
|
||||
{"unknown scope, no label", "weird", nil, false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := InUpdateScope(tt.scope, tt.labels); got != tt.want {
|
||||
t.Errorf("InUpdateScope(%q, %v) = %v, want %v", tt.scope, tt.labels, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsMonitorOnly(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
labels map[string]string
|
||||
want bool
|
||||
}{
|
||||
{"no labels", nil, false},
|
||||
{"primary true", map[string]string{labelUpdateMonitorOnly: "true"}, true},
|
||||
{"watchtower alias true", map[string]string{labelUpdateMonitorOnlyAlias: "true"}, true},
|
||||
{"primary false", map[string]string{labelUpdateMonitorOnly: "false"}, false},
|
||||
{"bad value", map[string]string{labelUpdateMonitorOnly: "maybe"}, false},
|
||||
{"primary wins over alias", map[string]string{labelUpdateMonitorOnly: "true", labelUpdateMonitorOnlyAlias: "false"}, true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsMonitorOnly(tt.labels); got != tt.want {
|
||||
t.Errorf("IsMonitorOnly(%v) = %v, want %v", tt.labels, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveContainerUpdateRouting(t *testing.T) {
|
||||
// stackLookup resolves "my-stack" to compose stack 7 (git) and nothing else,
|
||||
// mirroring how the job builds a per-endpoint compose-stack index.
|
||||
stackLookup := func(project string) *StackMatch {
|
||||
if project == "my-stack" {
|
||||
return &StackMatch{StackID: 7, IsGit: true}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
labels map[string]string
|
||||
want UpdateRouting
|
||||
}{
|
||||
{
|
||||
name: "no compose label -> standalone",
|
||||
labels: map[string]string{"foo": "bar"},
|
||||
want: UpdateRouting{Kind: UpdateStandalone},
|
||||
},
|
||||
{
|
||||
name: "empty compose label -> standalone",
|
||||
labels: map[string]string{composeProjectLabel: ""},
|
||||
want: UpdateRouting{Kind: UpdateStandalone},
|
||||
},
|
||||
{
|
||||
name: "compose project matching a portainer compose stack -> stack",
|
||||
labels: map[string]string{composeProjectLabel: "my-stack"},
|
||||
want: UpdateRouting{Kind: UpdateStack, StackID: 7, IsGit: true},
|
||||
},
|
||||
{
|
||||
name: "compose project with no matching stack -> external",
|
||||
labels: map[string]string{composeProjectLabel: "other"},
|
||||
want: UpdateRouting{Kind: UpdateExternal},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := resolveContainerUpdateRouting(tt.labels, stackLookup)
|
||||
if got != tt.want {
|
||||
t.Errorf("resolveContainerUpdateRouting(%v) = %+v, want %+v", tt.labels, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroupContainersForUpdate(t *testing.T) {
|
||||
// stackLookup: "web" -> compose stack 3 (file), "api" -> compose stack 4 (git).
|
||||
stackLookup := func(project string) *StackMatch {
|
||||
switch project {
|
||||
case "web":
|
||||
return &StackMatch{StackID: 3, IsGit: false}
|
||||
case "api":
|
||||
return &StackMatch{StackID: 4, IsGit: true}
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
candidates := []UpdateCandidate{
|
||||
{ID: "standalone-1"},
|
||||
{ID: "web-a", Name: "web-a", Labels: map[string]string{composeProjectLabel: "web"}},
|
||||
{ID: "web-b", Name: "web-b", Labels: map[string]string{composeProjectLabel: "web"}}, // same stack -> deduped redeploy, both kept as members
|
||||
{ID: "api-a", Name: "api-a", Labels: map[string]string{composeProjectLabel: "api"}},
|
||||
{ID: "ext-1", Labels: map[string]string{composeProjectLabel: "unknown"}},
|
||||
}
|
||||
|
||||
grouped := groupContainersForUpdate(candidates, stackLookup)
|
||||
|
||||
if len(grouped.Standalone) != 1 || grouped.Standalone[0].ID != "standalone-1" {
|
||||
t.Errorf("Standalone = %+v, want one entry standalone-1", grouped.Standalone)
|
||||
}
|
||||
|
||||
if len(grouped.External) != 1 || grouped.External[0].ID != "ext-1" {
|
||||
t.Errorf("External = %+v, want one entry ext-1", grouped.External)
|
||||
}
|
||||
|
||||
// One redeploy per stack: web appears twice in input but once in output.
|
||||
if len(grouped.Stacks) != 2 {
|
||||
t.Fatalf("Stacks = %+v, want 2 deduped stacks", grouped.Stacks)
|
||||
}
|
||||
|
||||
got := map[int]bool{}
|
||||
for _, st := range grouped.Stacks {
|
||||
got[st.StackID] = st.IsGit
|
||||
}
|
||||
|
||||
if isGit, ok := got[3]; !ok || isGit {
|
||||
t.Errorf("stack 3 = (%v, present=%v), want present file stack", isGit, ok)
|
||||
}
|
||||
|
||||
if isGit, ok := got[4]; !ok || !isGit {
|
||||
t.Errorf("stack 4 = (%v, present=%v), want present git stack", isGit, ok)
|
||||
}
|
||||
|
||||
// The stack is redeployed once, but every member container is threaded through
|
||||
// (not discarded at the collapse) so each can emit its own notification.
|
||||
members := map[int][]string{}
|
||||
for _, st := range grouped.Stacks {
|
||||
for _, c := range st.Containers {
|
||||
members[st.StackID] = append(members[st.StackID], c.Name)
|
||||
}
|
||||
}
|
||||
|
||||
if got := members[3]; len(got) != 2 || got[0] != "web-a" || got[1] != "web-b" {
|
||||
t.Errorf("stack 3 members = %v, want [web-a web-b]", got)
|
||||
}
|
||||
|
||||
if got := members[4]; len(got) != 1 || got[0] != "api-a" {
|
||||
t.Errorf("stack 4 members = %v, want [api-a]", got)
|
||||
}
|
||||
}
|
||||
@@ -1,113 +0,0 @@
|
||||
package containerautomation
|
||||
|
||||
import "github.com/rs/zerolog/log"
|
||||
|
||||
// EventKind enumerates the container-automation events surfaced to a Notifier.
|
||||
// The set is intentionally small: it is the seam future milestones extend with
|
||||
// real senders (Slack/email/webhook) without touching the daemon call sites.
|
||||
type EventKind string
|
||||
|
||||
const (
|
||||
// EventUpdated is emitted after a container/stack image was updated.
|
||||
EventUpdated EventKind = "updated"
|
||||
// EventRollback is emitted after a health-gated rollback to the previous image.
|
||||
EventRollback EventKind = "rollback"
|
||||
// EventUpdateFailed is emitted when an update (or its rollback) could not be applied.
|
||||
EventUpdateFailed EventKind = "update-failed"
|
||||
// EventHealRestarted is emitted after an unhealthy container was restarted.
|
||||
EventHealRestarted EventKind = "heal-restarted"
|
||||
)
|
||||
|
||||
// Event is a structured container-automation notification. Optional fields are
|
||||
// left zero when not applicable to the event (e.g. StackID for a standalone
|
||||
// update, ContainerID for a stack redeploy).
|
||||
type Event struct {
|
||||
Kind EventKind
|
||||
EndpointID int
|
||||
ContainerID string
|
||||
// ContainerName is the human-readable container name (no leading slash), used
|
||||
// by the webhook message. It may be empty for events keyed only by ID.
|
||||
ContainerName string
|
||||
StackID int
|
||||
// StackName is the compose project (stack) name a container belongs to, sourced
|
||||
// from its com.docker.compose.project label at detection time. It is set on a
|
||||
// per-container update event for a stack member so the webhook can print a
|
||||
// "Stack [name]" line without a StackID/Stack().Read round-trip; empty for
|
||||
// standalone containers.
|
||||
StackName string
|
||||
Image string
|
||||
// OldDigest and NewDigest carry the pre/post image identities for an update
|
||||
// (image IDs, e.g. "sha256:59b9..."). They are threaded from the update call
|
||||
// site where they are known and left empty otherwise; the webhook notifier
|
||||
// short-forms them into the "old → new" part of the message.
|
||||
OldDigest string
|
||||
NewDigest string
|
||||
Message string
|
||||
// Err carries the underlying error for failure events; nil otherwise.
|
||||
Err error
|
||||
}
|
||||
|
||||
// Notifier receives container-automation events. CE has no generic notification
|
||||
// subsystem, so the only implementation is logNotifier; this interface is the
|
||||
// seam external senders plug into later.
|
||||
type Notifier interface {
|
||||
Notify(event Event)
|
||||
}
|
||||
|
||||
// logNotifier is the default Notifier: it emits each event as a structured log
|
||||
// line. It never blocks and never errors, so it is safe to call from the daemon
|
||||
// hot path.
|
||||
type logNotifier struct{}
|
||||
|
||||
// Notify logs the event with its kind and context fields. Failure events are
|
||||
// logged at warn (with the error), the rest at info.
|
||||
func (logNotifier) Notify(event Event) {
|
||||
entry := log.Info()
|
||||
if event.Kind == EventUpdateFailed {
|
||||
entry = log.Warn()
|
||||
if event.Err != nil {
|
||||
entry = entry.Err(event.Err)
|
||||
}
|
||||
}
|
||||
|
||||
entry = entry.Str("event", string(event.Kind)).Int("endpoint_id", event.EndpointID)
|
||||
if event.ContainerID != "" {
|
||||
entry = entry.Str("container_id", event.ContainerID)
|
||||
}
|
||||
if event.StackID != 0 {
|
||||
entry = entry.Int("stack_id", event.StackID)
|
||||
}
|
||||
if event.Image != "" {
|
||||
entry = entry.Str("image", event.Image)
|
||||
}
|
||||
|
||||
message := event.Message
|
||||
if message == "" {
|
||||
message = "container automation event"
|
||||
}
|
||||
|
||||
entry.Msg("container automation: " + message)
|
||||
}
|
||||
|
||||
// multiNotifier fans an event out to several notifiers in order. It is how the
|
||||
// service composes the always-on logNotifier with the optional webhookNotifier
|
||||
// without either implementation having to know about the other. Each notifier is
|
||||
// itself non-blocking, so multiNotifier stays safe on the daemon hot path.
|
||||
type multiNotifier []Notifier
|
||||
|
||||
// Notify forwards the event to every wrapped notifier. Each call is isolated by
|
||||
// a recover() so one misbehaving notifier can neither abort the others nor let a
|
||||
// panic reach the daemon hot path; logNotifier is kept first and unchanged.
|
||||
func (m multiNotifier) Notify(event Event) {
|
||||
for _, n := range m {
|
||||
func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Warn().Interface("panic", r).Msg("container automation: recovered from panic in notifier")
|
||||
}
|
||||
}()
|
||||
|
||||
n.Notify(event)
|
||||
}()
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
package containerautomation
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
)
|
||||
|
||||
// recordingNotifier captures emitted events for assertions in tests.
|
||||
type recordingNotifier struct {
|
||||
events []Event
|
||||
}
|
||||
|
||||
func (r *recordingNotifier) Notify(event Event) {
|
||||
r.events = append(r.events, event)
|
||||
}
|
||||
|
||||
func TestLogNotifierDoesNotPanic(t *testing.T) {
|
||||
n := logNotifier{}
|
||||
|
||||
// Every event kind, including a failure carrying an error, must log without
|
||||
// panicking and without requiring any optional field.
|
||||
n.Notify(Event{Kind: EventUpdated, EndpointID: 1, ContainerID: "abc", Image: "nginx:latest"})
|
||||
n.Notify(Event{Kind: EventUpdated, EndpointID: 1, StackID: 7})
|
||||
n.Notify(Event{Kind: EventRollback, EndpointID: 2, ContainerID: "def", Image: "nginx:1.0"})
|
||||
n.Notify(Event{Kind: EventHealRestarted, EndpointID: 3, ContainerID: "ghi"})
|
||||
n.Notify(Event{Kind: EventUpdateFailed, EndpointID: 4, ContainerID: "jkl", Err: errors.New("boom")})
|
||||
n.Notify(Event{Kind: EventUpdateFailed, EndpointID: 4}) // failure without an error
|
||||
n.Notify(Event{}) // zero value
|
||||
}
|
||||
|
||||
func TestRecordingNotifierCapturesEvents(t *testing.T) {
|
||||
r := &recordingNotifier{}
|
||||
r.Notify(Event{Kind: EventUpdated, EndpointID: 1})
|
||||
r.Notify(Event{Kind: EventRollback, EndpointID: 1})
|
||||
|
||||
if len(r.events) != 2 {
|
||||
t.Fatalf("captured %d events, want 2", len(r.events))
|
||||
}
|
||||
if r.events[0].Kind != EventUpdated || r.events[1].Kind != EventRollback {
|
||||
t.Errorf("unexpected event kinds: %v, %v", r.events[0].Kind, r.events[1].Kind)
|
||||
}
|
||||
}
|
||||
|
||||
// panicNotifier always panics, standing in for a misbehaving notifier.
|
||||
type panicNotifier struct{}
|
||||
|
||||
func (panicNotifier) Notify(Event) {
|
||||
panic("boom")
|
||||
}
|
||||
|
||||
// TestMultiNotifierIsolatesPanics verifies a panicking notifier neither aborts
|
||||
// the sibling notifiers nor lets the panic reach the caller.
|
||||
func TestMultiNotifierIsolatesPanics(t *testing.T) {
|
||||
before := &recordingNotifier{}
|
||||
after := &recordingNotifier{}
|
||||
|
||||
m := multiNotifier{before, panicNotifier{}, after}
|
||||
|
||||
// Must not panic even though a wrapped notifier does.
|
||||
m.Notify(Event{Kind: EventUpdated, EndpointID: 1})
|
||||
|
||||
if len(before.events) != 1 {
|
||||
t.Errorf("notifier before the panicking one got %d events, want 1", len(before.events))
|
||||
}
|
||||
if len(after.events) != 1 {
|
||||
t.Errorf("notifier after the panicking one got %d events, want 1 (panic must not abort the loop)", len(after.events))
|
||||
}
|
||||
}
|
||||
|
||||
func TestAutomationEnabledForEndpoint(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
endpoint *portainer.Endpoint
|
||||
want bool
|
||||
}{
|
||||
{name: "nil endpoint is not enabled", endpoint: nil, want: false},
|
||||
{name: "default (zero value) participates", endpoint: &portainer.Endpoint{}, want: true},
|
||||
{name: "explicitly disabled opts out", endpoint: &portainer.Endpoint{ContainerAutomationDisabled: true}, want: false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := AutomationEnabledForEndpoint(tt.endpoint); got != tt.want {
|
||||
t.Errorf("AutomationEnabledForEndpoint() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,387 +0,0 @@
|
||||
package containerautomation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
dockerclient "github.com/docker/docker/client"
|
||||
"github.com/rs/zerolog/log"
|
||||
"go.podman.io/image/v5/docker/reference"
|
||||
)
|
||||
|
||||
const (
|
||||
// defaultRollbackTimeout bounds how long the health gate waits for a freshly
|
||||
// updated standalone container to become healthy before rolling back.
|
||||
defaultRollbackTimeout = 120 * time.Second
|
||||
// rollbackPollInterval is the delay between two health probes of the new
|
||||
// container while the rollback window is open.
|
||||
rollbackPollInterval = 3 * time.Second
|
||||
// rollbackGateBuffer is added to the rollback timeout when deriving the inspect
|
||||
// context deadline, leaving room for the final probe to complete after the
|
||||
// decision deadline elapses.
|
||||
rollbackGateBuffer = 10 * time.Second
|
||||
// startPeriodBuffer is added to a container's healthcheck start_period when it
|
||||
// is longer than the rollback timeout, so the gate waits through the whole
|
||||
// start period (during which Docker reports "starting") plus a small grace
|
||||
// before deciding. Without it a legitimately slow-starting container would be
|
||||
// rolled back while it is still initializing normally.
|
||||
startPeriodBuffer = 15 * time.Second
|
||||
// maxConsecutiveInspectErrors is how many back-to-back inspect failures the
|
||||
// health gate tolerates before declaring the update failed. A single transient
|
||||
// Docker API blip must not trigger a false rollback, so the gate keeps polling
|
||||
// and only gives up once the failures are clearly not transient.
|
||||
maxConsecutiveInspectErrors = 3
|
||||
// updateRollbackCooldown is how long a standalone container whose update was
|
||||
// rolled back is skipped from updating to the SAME failed image again. It
|
||||
// breaks the update->rollback loop: without it a persistently-unhealthy new
|
||||
// image would be re-pulled and rolled back on every poll tick. A genuinely new
|
||||
// upstream image (a changed remote digest) is not blocked; the cooldown only
|
||||
// suppresses the exact target that just failed. It is generous because a broken
|
||||
// upstream image is normally fixed by a new push, which lifts the skip at once.
|
||||
updateRollbackCooldown = 24 * time.Hour
|
||||
)
|
||||
|
||||
// rolledBackTarget records that a standalone container's update to a specific
|
||||
// remote image was rolled back, so the same target is skipped until the cooldown
|
||||
// elapses or the upstream digest changes.
|
||||
type rolledBackTarget struct {
|
||||
// ref is the container's original image reference (the re-tag target), used to
|
||||
// re-resolve the current remote digest on later ticks.
|
||||
ref string
|
||||
// digest is the remote image digest that failed the health gate. A later tick
|
||||
// resolving a DIFFERENT digest (a new upstream push) is allowed through; the
|
||||
// same digest is skipped until the cooldown elapses. Empty when it could not be
|
||||
// resolved at rollback time, in which case the guard skips conservatively.
|
||||
digest string
|
||||
// at is when the rollback happened; the cooldown is measured from it.
|
||||
at time.Time
|
||||
}
|
||||
|
||||
// decideUpdateSkip is the pure core of the update->rollback loop guard: given a
|
||||
// recorded rolled-back target and the freshly-resolved current remote digest, it
|
||||
// reports whether the standalone update must be skipped this tick. The skip holds
|
||||
// only while the cooldown is open AND the remote still points at the same failed
|
||||
// image; once the cooldown elapses the skip is lifted. An unknown recorded digest
|
||||
// is skipped conservatively (we cannot prove the target changed). Mirrors the
|
||||
// decideRestart pattern so it is unit-testable without Docker.
|
||||
func decideUpdateSkip(rec rolledBackTarget, currentDigest string, now time.Time, cooldown time.Duration) bool {
|
||||
if now.Sub(rec.at) >= cooldown {
|
||||
return false
|
||||
}
|
||||
|
||||
if rec.digest == "" {
|
||||
return true
|
||||
}
|
||||
|
||||
return currentDigest == rec.digest
|
||||
}
|
||||
|
||||
// rollbackOutcome is the decision produced from a single health sample.
|
||||
type rollbackOutcome int
|
||||
|
||||
const (
|
||||
// rollbackContinue: still starting and before the deadline, keep polling.
|
||||
rollbackContinue rollbackOutcome = iota
|
||||
// rollbackHealthy: the new container is healthy, accept the update.
|
||||
rollbackHealthy
|
||||
// rollbackTrigger: the new container failed the health gate, roll back.
|
||||
rollbackTrigger
|
||||
)
|
||||
|
||||
// gateResult is the terminal outcome of healthGate. It is a tri-state because a
|
||||
// shutdown mid-gate must be distinguished from a genuine failure: only a real
|
||||
// unhealthy/not-running/deadline outcome may roll back.
|
||||
type gateResult int
|
||||
|
||||
const (
|
||||
// gateHealthy: the new container became healthy in time, accept the update.
|
||||
gateHealthy gateResult = iota
|
||||
// gateRollback: the new container failed the gate, roll back to the old image.
|
||||
gateRollback
|
||||
// gateAborted: the service base context was cancelled (server shutdown) while
|
||||
// the gate was open. The new container is left running as-is; no rollback and
|
||||
// no failure event, since we never observed an actual failure.
|
||||
gateAborted
|
||||
)
|
||||
|
||||
// imageIDReference matches a content-addressable image id carried verbatim in a
|
||||
// container's Config.Image when it was started from a bare id (e.g.
|
||||
// "sha256:ab12…"). Such an id is not a tag and cannot be re-tagged, so it must
|
||||
// not enable the health gate. A full bare hex id (no algorithm prefix) is
|
||||
// already rejected by reference.ParseNormalizedNamed; this catches the
|
||||
// algorithm-prefixed digest form, which otherwise parses as a bogus tag.
|
||||
var imageIDReference = regexp.MustCompile(`^[a-z0-9]+:[0-9a-f]{64}$`)
|
||||
|
||||
// containerHealth is the minimal health signal the gate polls. It is built from
|
||||
// a container inspect but kept independent of the Docker SDK so the decision
|
||||
// logic can be unit-tested without a Docker engine.
|
||||
type containerHealth struct {
|
||||
// Running reports whether the container is currently running. A container that
|
||||
// has exited within the window is a failed update.
|
||||
Running bool
|
||||
// Status is the Docker health status: "starting", "healthy", "unhealthy" or
|
||||
// "none"/"" when there is no healthcheck.
|
||||
Status string
|
||||
}
|
||||
|
||||
// decideRollback is a pure decision over a single health sample taken at time
|
||||
// `now`, given the rollback `deadline`. It is the testable core of the health
|
||||
// gate: callers feed it successive samples and act on the outcome.
|
||||
//
|
||||
// Rules, in order:
|
||||
// - healthy -> accept the update (rollbackHealthy);
|
||||
// - unhealthy -> roll back immediately (Docker only reports unhealthy after the
|
||||
// configured retries fail, so it is a definitive signal);
|
||||
// - not running (crashed/exited post-start) -> roll back;
|
||||
// - still starting past the deadline -> roll back (never became healthy in time);
|
||||
// - otherwise keep waiting (rollbackContinue).
|
||||
func decideRollback(h containerHealth, now, deadline time.Time) rollbackOutcome {
|
||||
switch h.Status {
|
||||
case string(container.Healthy):
|
||||
return rollbackHealthy
|
||||
case string(container.Unhealthy):
|
||||
return rollbackTrigger
|
||||
}
|
||||
|
||||
if !h.Running {
|
||||
return rollbackTrigger
|
||||
}
|
||||
|
||||
if !now.Before(deadline) {
|
||||
return rollbackTrigger
|
||||
}
|
||||
|
||||
return rollbackContinue
|
||||
}
|
||||
|
||||
// effectiveRollbackDeadline derives the health-gate deadline from the gate start
|
||||
// time, the configured rollback timeout, and the container's healthcheck
|
||||
// start_period. While a container is within its start_period Docker keeps
|
||||
// reporting "starting" (it never reports unhealthy yet), so a start_period
|
||||
// longer than the rollback timeout would otherwise trip a premature rollback
|
||||
// while the container is initializing normally. The deadline is therefore the
|
||||
// later of (start + timeout) and (start + start_period + buffer).
|
||||
func effectiveRollbackDeadline(start time.Time, timeout, startPeriod time.Duration) time.Time {
|
||||
window := timeout
|
||||
if startPeriod > 0 {
|
||||
if d := startPeriod + startPeriodBuffer; d > window {
|
||||
window = d
|
||||
}
|
||||
}
|
||||
|
||||
return start.Add(window)
|
||||
}
|
||||
|
||||
// inspectErrorTolerated reports whether the health gate should keep polling after
|
||||
// `consecutive` back-to-back inspect failures rather than declaring the update
|
||||
// failed. Up to maxConsecutiveInspectErrors transient errors are tolerated; the
|
||||
// counter is reset by the caller on any successful inspect.
|
||||
func inspectErrorTolerated(consecutive int) bool {
|
||||
return consecutive <= maxConsecutiveInspectErrors
|
||||
}
|
||||
|
||||
// hasHealthGate reports whether a container's healthcheck config yields a usable
|
||||
// health signal. A nil config, an empty test, or an explicit {"NONE"} disable all
|
||||
// mean Docker never reports healthy/unhealthy, so there is nothing to gate on.
|
||||
func hasHealthGate(hc *container.HealthConfig) bool {
|
||||
if hc == nil || len(hc.Test) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
return hc.Test[0] != "NONE"
|
||||
}
|
||||
|
||||
// isTagReference reports whether ref is a proper tag reference that the health
|
||||
// gate can roll back. Rolling back re-tags the previous image id onto ref via
|
||||
// ImageTag, which Docker rejects for a digest-pinned reference (repo@sha256:…)
|
||||
// with "refusing to create a tag with a digest reference", and which is
|
||||
// meaningless for a bare image id. Such containers are detected here so the gate
|
||||
// is skipped instead of silently no-op'ing.
|
||||
func isTagReference(ref string) bool {
|
||||
if ref == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
// Algorithm-prefixed image id (e.g. "sha256:<64 hex>"): a bare id, not a tag.
|
||||
if imageIDReference.MatchString(ref) {
|
||||
return false
|
||||
}
|
||||
|
||||
named, err := reference.ParseNormalizedNamed(ref)
|
||||
if err != nil {
|
||||
// Unparseable (e.g. a full bare hex image id): not a usable tag target.
|
||||
return false
|
||||
}
|
||||
|
||||
// A digest-pinned reference (with or without a tag) cannot be re-tagged.
|
||||
if _, ok := named.(reference.Canonical); ok {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
// healthGate polls the new container's health until it becomes healthy, fails, or
|
||||
// the rollback window elapses, returning the terminal gateResult.
|
||||
//
|
||||
// The polling context is derived from the service base context, so a server
|
||||
// shutdown ends the wait. A shutdown is reported as gateAborted (leave the new
|
||||
// container in place, do not roll back): we never observed a real failure, and a
|
||||
// rollback derived from the cancelled context would itself fail and emit a
|
||||
// misleading "rollback failed" event on every shutdown during a gate window.
|
||||
//
|
||||
// Transient inspect failures (a brief Docker API blip) are tolerated: the gate
|
||||
// keeps polling and only declares the update failed after more than
|
||||
// maxConsecutiveInspectErrors consecutive failures, resetting on any success.
|
||||
//
|
||||
// Scheduling note (known limitation): this poll runs inside the sequential update
|
||||
// tick, so N unhealthy standalone containers with rollback enabled can each hold
|
||||
// the tick for up to their rollback window, delaying other containers/endpoints
|
||||
// in the same tick. The overlap guard in update() still prevents ticks from
|
||||
// piling up; this is accepted rather than re-architected (no per-container
|
||||
// goroutine) to keep the update path simple and ordered.
|
||||
func (s *Service) healthGate(cli *dockerclient.Client, containerID string, timeout, startPeriod time.Duration) gateResult {
|
||||
if timeout <= 0 {
|
||||
timeout = defaultRollbackTimeout
|
||||
}
|
||||
|
||||
deadline := effectiveRollbackDeadline(time.Now(), timeout, startPeriod)
|
||||
|
||||
ctx, cancel := context.WithDeadline(s.baseCtx, deadline.Add(rollbackGateBuffer))
|
||||
defer cancel()
|
||||
|
||||
consecutiveErrors := 0
|
||||
for {
|
||||
inspect, err := cli.ContainerInspect(ctx, containerID)
|
||||
if err != nil {
|
||||
// Server shutdown cancelled the base context: abort without rolling back.
|
||||
if errors.Is(ctx.Err(), context.Canceled) || errors.Is(s.baseCtx.Err(), context.Canceled) {
|
||||
log.Debug().Str("container_id", containerID).
|
||||
Msg("auto-update: health gate aborted due to shutdown")
|
||||
|
||||
return gateAborted
|
||||
}
|
||||
|
||||
consecutiveErrors++
|
||||
if !inspectErrorTolerated(consecutiveErrors) {
|
||||
// Repeated failures: the container vanished or the engine is
|
||||
// unreachable, treat as a failed update so the rollback can restore
|
||||
// the previous image.
|
||||
log.Warn().Err(err).Str("container_id", containerID).Int("consecutive_errors", consecutiveErrors).
|
||||
Msg("auto-update: health gate inspect failed repeatedly, treating as unhealthy")
|
||||
|
||||
return gateRollback
|
||||
}
|
||||
|
||||
// Tolerate a transient blip: keep polling until the data resolves or the
|
||||
// deadline passes.
|
||||
log.Debug().Err(err).Str("container_id", containerID).Int("consecutive_errors", consecutiveErrors).
|
||||
Msg("auto-update: health gate inspect failed, retrying (transient)")
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return s.gateDeadlineResult()
|
||||
case <-time.After(rollbackPollInterval):
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
consecutiveErrors = 0
|
||||
|
||||
h := containerHealth{Running: inspect.State != nil && inspect.State.Running}
|
||||
if inspect.State != nil && inspect.State.Health != nil {
|
||||
h.Status = string(inspect.State.Health.Status)
|
||||
}
|
||||
|
||||
switch decideRollback(h, time.Now(), deadline) {
|
||||
case rollbackHealthy:
|
||||
return gateHealthy
|
||||
case rollbackTrigger:
|
||||
return gateRollback
|
||||
}
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return s.gateDeadlineResult()
|
||||
case <-time.After(rollbackPollInterval):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// gateDeadlineResult maps a context-done gate exit to its outcome: a base-context
|
||||
// cancellation (shutdown) aborts without rolling back, while a plain deadline
|
||||
// (the container never became healthy in time) rolls back.
|
||||
func (s *Service) gateDeadlineResult() gateResult {
|
||||
if errors.Is(s.baseCtx.Err(), context.Canceled) {
|
||||
log.Debug().Msg("auto-update: health gate aborted due to shutdown")
|
||||
|
||||
return gateAborted
|
||||
}
|
||||
|
||||
return gateRollback
|
||||
}
|
||||
|
||||
// rollback restores the previous image after a failed health-gated update. It
|
||||
// re-tags the old image id back onto the container's original reference (which
|
||||
// the new image currently owns), then recreates the new container on that
|
||||
// reference with no pull, so Recreate's full config-preservation + create-failure
|
||||
// rollback is reused while resolving to the old image.
|
||||
//
|
||||
// Side effect: re-tagging moves `originalRef` from the new image to the old one,
|
||||
// leaving the new (unhealthy) image untagged/dangling. It is intentionally left
|
||||
// in place (not pruned) so an operator can inspect why the update failed.
|
||||
//
|
||||
// If any step fails the previous image cannot be safely restored, so the
|
||||
// (unhealthy) new container is left running rather than destroyed, and a loud
|
||||
// failure notification is emitted.
|
||||
func (s *Service) rollback(cli *dockerclient.Client, endpoint *portainer.Endpoint, newContainerID, oldImageID, originalRef, containerName string) {
|
||||
endpointID := int(endpoint.ID)
|
||||
|
||||
log.Warn().Str("container_id", newContainerID).Str("image", originalRef).Int("endpoint_id", endpointID).
|
||||
Msg("auto-update: new container failed the health gate, rolling back to the previous image")
|
||||
|
||||
ctx, cancel := context.WithTimeout(s.baseCtx, recreateTimeout)
|
||||
defer cancel()
|
||||
|
||||
// Re-tag the previous image id back onto the original reference. After the
|
||||
// update the reference points at the new image; this moves it back so Recreate
|
||||
// resolves the old image without a pull.
|
||||
if err := cli.ImageTag(ctx, oldImageID, originalRef); err != nil {
|
||||
log.Error().Err(err).Str("image_id", oldImageID).Str("image", originalRef).Int("endpoint_id", endpointID).
|
||||
Msg("auto-update: rollback failed to re-tag the previous image, leaving the unhealthy container in place")
|
||||
s.notifier.Notify(Event{
|
||||
Kind: EventUpdateFailed, EndpointID: endpointID, ContainerID: newContainerID, ContainerName: containerName,
|
||||
Image: originalRef, Message: "rollback failed: could not re-tag previous image", Err: err,
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := s.containerService.Recreate(ctx, endpoint, newContainerID, false, "", ""); err != nil {
|
||||
log.Error().Err(err).Str("container_id", newContainerID).Str("image", originalRef).Int("endpoint_id", endpointID).
|
||||
Msg("auto-update: rollback recreate failed, leaving the unhealthy container in place")
|
||||
s.notifier.Notify(Event{
|
||||
Kind: EventUpdateFailed, EndpointID: endpointID, ContainerID: newContainerID, ContainerName: containerName,
|
||||
Image: originalRef, Message: "rollback failed: could not recreate on previous image", Err: err,
|
||||
})
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
log.Warn().Str("container_id", newContainerID).Str("image", originalRef).Int("endpoint_id", endpointID).
|
||||
Msg("auto-update: rolled back to the previous image after a failed update")
|
||||
s.notifier.Notify(Event{
|
||||
Kind: EventRollback, EndpointID: endpointID, ContainerID: newContainerID, ContainerName: containerName,
|
||||
Image: originalRef, Message: "rolled back to previous image after failed health check",
|
||||
})
|
||||
|
||||
// Record the failed target so the next poll does not immediately re-pull the
|
||||
// same broken image and roll back again (the update->rollback loop). Recorded
|
||||
// only after a SUCCESSFUL rollback; a changed remote digest later lifts the skip.
|
||||
s.recordRolledBack(endpoint, containerName, originalRef)
|
||||
}
|
||||
@@ -1,333 +0,0 @@
|
||||
package containerautomation
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api/types/container"
|
||||
)
|
||||
|
||||
func TestDecideRollback(t *testing.T) {
|
||||
now := time.Date(2026, 6, 28, 12, 0, 0, 0, time.UTC)
|
||||
deadline := now.Add(120 * time.Second)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
health containerHealth
|
||||
at time.Time
|
||||
want rollbackOutcome
|
||||
}{
|
||||
{
|
||||
name: "healthy within the window accepts the update",
|
||||
health: containerHealth{Running: true, Status: string(container.Healthy)},
|
||||
at: now.Add(10 * time.Second),
|
||||
want: rollbackHealthy,
|
||||
},
|
||||
{
|
||||
name: "unhealthy triggers an immediate rollback",
|
||||
health: containerHealth{Running: true, Status: string(container.Unhealthy)},
|
||||
at: now.Add(10 * time.Second),
|
||||
want: rollbackTrigger,
|
||||
},
|
||||
{
|
||||
name: "still starting before the deadline keeps polling",
|
||||
health: containerHealth{Running: true, Status: string(container.Starting)},
|
||||
at: now.Add(10 * time.Second),
|
||||
want: rollbackContinue,
|
||||
},
|
||||
{
|
||||
name: "still starting past the deadline rolls back",
|
||||
health: containerHealth{Running: true, Status: string(container.Starting)},
|
||||
at: now.Add(121 * time.Second),
|
||||
want: rollbackTrigger,
|
||||
},
|
||||
{
|
||||
name: "starting exactly at the deadline rolls back",
|
||||
health: containerHealth{Running: true, Status: string(container.Starting)},
|
||||
at: deadline,
|
||||
want: rollbackTrigger,
|
||||
},
|
||||
{
|
||||
name: "exited container rolls back even before the deadline",
|
||||
health: containerHealth{Running: false, Status: string(container.Starting)},
|
||||
at: now.Add(5 * time.Second),
|
||||
want: rollbackTrigger,
|
||||
},
|
||||
{
|
||||
name: "unhealthy wins over a stopped state",
|
||||
health: containerHealth{Running: false, Status: string(container.Unhealthy)},
|
||||
at: now.Add(5 * time.Second),
|
||||
want: rollbackTrigger,
|
||||
},
|
||||
{
|
||||
name: "healthy wins even past the deadline",
|
||||
health: containerHealth{Running: true, Status: string(container.Healthy)},
|
||||
at: now.Add(200 * time.Second),
|
||||
want: rollbackHealthy,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := decideRollback(tt.health, tt.at, deadline); got != tt.want {
|
||||
t.Errorf("decideRollback() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEffectiveRollbackDeadline(t *testing.T) {
|
||||
start := time.Date(2026, 6, 28, 12, 0, 0, 0, time.UTC)
|
||||
timeout := 120 * time.Second
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
startPeriod time.Duration
|
||||
want time.Time
|
||||
}{
|
||||
{
|
||||
name: "no start period uses the timeout",
|
||||
startPeriod: 0,
|
||||
want: start.Add(timeout),
|
||||
},
|
||||
{
|
||||
name: "start period shorter than timeout uses the timeout",
|
||||
startPeriod: 30 * time.Second,
|
||||
want: start.Add(timeout),
|
||||
},
|
||||
{
|
||||
name: "start period longer than timeout extends to start period plus buffer",
|
||||
startPeriod: 300 * time.Second,
|
||||
want: start.Add(300*time.Second + startPeriodBuffer),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := effectiveRollbackDeadline(start, timeout, tt.startPeriod); !got.Equal(tt.want) {
|
||||
t.Errorf("effectiveRollbackDeadline() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestDecideRollbackWithLongStartPeriod proves the F3 fix end to end at the
|
||||
// decision layer: with a start_period longer than the configured rollback
|
||||
// timeout, the start-period-aware deadline keeps a still-starting container
|
||||
// alive while it is within the start period, and only rolls back after it.
|
||||
func TestDecideRollbackWithLongStartPeriod(t *testing.T) {
|
||||
start := time.Date(2026, 6, 28, 12, 0, 0, 0, time.UTC)
|
||||
timeout := 60 * time.Second
|
||||
startPeriod := 300 * time.Second
|
||||
|
||||
deadline := effectiveRollbackDeadline(start, timeout, startPeriod)
|
||||
|
||||
starting := containerHealth{Running: true, Status: string(container.Starting)}
|
||||
|
||||
// Past the bare timeout but still within the start period: keep waiting.
|
||||
if got := decideRollback(starting, start.Add(120*time.Second), deadline); got != rollbackContinue {
|
||||
t.Errorf("within start_period: decideRollback() = %v, want rollbackContinue", got)
|
||||
}
|
||||
|
||||
// After the start period (plus buffer): roll back.
|
||||
if got := decideRollback(starting, start.Add(330*time.Second), deadline); got != rollbackTrigger {
|
||||
t.Errorf("after start_period: decideRollback() = %v, want rollbackTrigger", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInspectErrorTolerated(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
consecutive int
|
||||
want bool
|
||||
}{
|
||||
{name: "first transient error is tolerated", consecutive: 1, want: true},
|
||||
{name: "second consecutive error is tolerated", consecutive: 2, want: true},
|
||||
{name: "at the threshold is still tolerated", consecutive: maxConsecutiveInspectErrors, want: true},
|
||||
{name: "beyond the threshold is a failure", consecutive: maxConsecutiveInspectErrors + 1, want: false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := inspectErrorTolerated(tt.consecutive); got != tt.want {
|
||||
t.Errorf("inspectErrorTolerated(%d) = %v, want %v", tt.consecutive, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsTagReference(t *testing.T) {
|
||||
const digest = "sha256:02c921df998f95e849058af14de7045efc3954d90320967418a0d1f182bbc0b2"
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
ref string
|
||||
want bool
|
||||
}{
|
||||
{name: "tagged reference is rollbackable", ref: "nginx:1.21", want: true},
|
||||
{name: "untagged reference (implicit latest) is rollbackable", ref: "nginx", want: true},
|
||||
{name: "fully-qualified tagged reference is rollbackable", ref: "registry.example.com/team/app:v2", want: true},
|
||||
{name: "digest-pinned reference cannot be re-tagged", ref: "nginx@" + digest, want: false},
|
||||
{name: "tagged-and-digest-pinned reference cannot be re-tagged", ref: "nginx:1.21@" + digest, want: false},
|
||||
{name: "algorithm-prefixed bare image id cannot be re-tagged", ref: digest, want: false},
|
||||
{name: "full bare hex image id cannot be re-tagged", ref: "02c921df998f95e849058af14de7045efc3954d90320967418a0d1f182bbc0b2", want: false},
|
||||
{name: "empty reference is not rollbackable", ref: "", want: false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := isTagReference(tt.ref); got != tt.want {
|
||||
t.Errorf("isTagReference(%q) = %v, want %v", tt.ref, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSkipUnnamedForRollback(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
rollback bool
|
||||
cName string
|
||||
want bool
|
||||
}{
|
||||
{name: "rollback on, unnamed -> skip (unsuppressable loop otherwise)", rollback: true, cName: "", want: true},
|
||||
{name: "rollback on, named -> proceed (guard can key it)", rollback: true, cName: "web", want: false},
|
||||
{name: "rollback off, unnamed -> proceed (no rollback to loop)", rollback: false, cName: "", want: false},
|
||||
{name: "rollback off, named -> proceed", rollback: false, cName: "web", want: false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := skipUnnamedForRollback(tt.rollback, tt.cName); got != tt.want {
|
||||
t.Errorf("skipUnnamedForRollback(%v, %q) = %v, want %v", tt.rollback, tt.cName, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestHasHealthGate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
hc *container.HealthConfig
|
||||
want bool
|
||||
}{
|
||||
{name: "nil config has no gate", hc: nil, want: false},
|
||||
{name: "empty test inherits, no usable gate", hc: &container.HealthConfig{Test: nil}, want: false},
|
||||
{name: "explicit NONE disables the gate", hc: &container.HealthConfig{Test: []string{"NONE"}}, want: false},
|
||||
{name: "CMD healthcheck yields a gate", hc: &container.HealthConfig{Test: []string{"CMD", "curl", "-f", "localhost"}}, want: true},
|
||||
{name: "CMD-SHELL healthcheck yields a gate", hc: &container.HealthConfig{Test: []string{"CMD-SHELL", "exit 0"}}, want: true},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := hasHealthGate(tt.hc); got != tt.want {
|
||||
t.Errorf("hasHealthGate() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRollbackTimeout(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
raw string
|
||||
want time.Duration
|
||||
}{
|
||||
{name: "valid duration", raw: "90s", want: 90 * time.Second},
|
||||
{name: "empty falls back to default", raw: "", want: defaultRollbackTimeout},
|
||||
{name: "unparseable falls back to default", raw: "nope", want: defaultRollbackTimeout},
|
||||
{name: "zero falls back to default", raw: "0s", want: defaultRollbackTimeout},
|
||||
{name: "negative falls back to default", raw: "-5s", want: defaultRollbackTimeout},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := parseRollbackTimeout(tt.raw); got != tt.want {
|
||||
t.Errorf("parseRollbackTimeout(%q) = %v, want %v", tt.raw, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecideUpdateSkip(t *testing.T) {
|
||||
now := time.Date(2026, 6, 28, 12, 0, 0, 0, time.UTC)
|
||||
cooldown := 24 * time.Hour
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
rec rolledBackTarget
|
||||
currentDigest string
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "same digest within cooldown is skipped",
|
||||
rec: rolledBackTarget{digest: "sha256:aaa", at: now.Add(-1 * time.Hour)},
|
||||
currentDigest: "sha256:aaa",
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "new digest within cooldown is not skipped",
|
||||
rec: rolledBackTarget{digest: "sha256:aaa", at: now.Add(-1 * time.Hour)},
|
||||
currentDigest: "sha256:bbb",
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "same digest after cooldown is not skipped",
|
||||
rec: rolledBackTarget{digest: "sha256:aaa", at: now.Add(-25 * time.Hour)},
|
||||
currentDigest: "sha256:aaa",
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "unknown recorded digest is skipped conservatively within cooldown",
|
||||
rec: rolledBackTarget{digest: "", at: now.Add(-1 * time.Hour)},
|
||||
currentDigest: "sha256:aaa",
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "unknown recorded digest after cooldown is not skipped",
|
||||
rec: rolledBackTarget{digest: "", at: now.Add(-25 * time.Hour)},
|
||||
currentDigest: "sha256:aaa",
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := decideUpdateSkip(tt.rec, tt.currentDigest, now, cooldown); got != tt.want {
|
||||
t.Errorf("decideUpdateSkip() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestPruneRolledBack locks in the F8 fix: pruneRolledBack must iterate the
|
||||
// rolledBack map and drop only entries whose cooldown has fully elapsed, keeping
|
||||
// fresh ones, so the map cannot grow unbounded. It mirrors TestPruneRetries. The
|
||||
// boundary is inclusive (production uses now.Sub(at) >= updateRollbackCooldown),
|
||||
// so an entry exactly at the cooldown is pruned.
|
||||
func TestPruneRolledBack(t *testing.T) {
|
||||
now := time.Date(2026, 6, 28, 12, 0, 0, 0, time.UTC)
|
||||
s := &Service{rolledBack: map[string]rolledBackTarget{
|
||||
// within the cooldown -> retained
|
||||
"fresh": {ref: "img:fresh", digest: "sha256:aaa", at: now.Add(-updateRollbackCooldown / 2)},
|
||||
// exactly at the cooldown boundary -> pruned (>= is inclusive)
|
||||
"edge": {ref: "img:edge", digest: "sha256:bbb", at: now.Add(-updateRollbackCooldown)},
|
||||
// long past the cooldown -> pruned
|
||||
"stale": {ref: "img:stale", digest: "sha256:ccc", at: now.Add(-2 * updateRollbackCooldown)},
|
||||
}}
|
||||
|
||||
s.pruneRolledBack(now)
|
||||
|
||||
if _, ok := s.rolledBack["fresh"]; !ok {
|
||||
t.Error("entry within the rollback cooldown should be retained")
|
||||
}
|
||||
if _, ok := s.rolledBack["edge"]; ok {
|
||||
t.Error("entry exactly at the cooldown boundary should be pruned")
|
||||
}
|
||||
if _, ok := s.rolledBack["stale"]; ok {
|
||||
t.Error("entry past the rollback cooldown should be pruned")
|
||||
}
|
||||
if len(s.rolledBack) != 1 {
|
||||
t.Errorf("rolledBack length = %d, want 1", len(s.rolledBack))
|
||||
}
|
||||
}
|
||||
@@ -1,318 +0,0 @@
|
||||
// Package containerautomation provides native container automation that runs as
|
||||
// background scheduler jobs. M1 implements auto-heal (restarting Docker
|
||||
// containers whose healthcheck reports "unhealthy", replacing the
|
||||
// willfarrell/autoheal sidecar); M4 adds auto-update (periodically detecting
|
||||
// outdated images and applying updates, replacing the containrrr/watchtower
|
||||
// sidecar).
|
||||
package containerautomation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/dataservices"
|
||||
"github.com/portainer/portainer/api/docker"
|
||||
dockerclient "github.com/portainer/portainer/api/docker/client"
|
||||
"github.com/portainer/portainer/api/docker/images"
|
||||
"github.com/portainer/portainer/api/scheduler"
|
||||
"github.com/portainer/portainer/api/stacks/deployments"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
const (
|
||||
// defaultCheckInterval is used when the configured auto-heal interval is empty or unparseable.
|
||||
defaultCheckInterval = 30 * time.Second
|
||||
// defaultPollInterval is used when the configured auto-update interval is empty or unparseable.
|
||||
// It is conservative (hours) to stay within registry rate limits; the image-status cache is
|
||||
// short-lived (keyed by the local imageID), so each poll re-checks the remote digest.
|
||||
defaultPollInterval = 6 * time.Hour
|
||||
)
|
||||
|
||||
// Service manages the lifecycle of the auto-heal and auto-update scheduler jobs
|
||||
// and keeps the per-container retry state in memory across ticks.
|
||||
type Service struct {
|
||||
// baseCtx is the application shutdown context. It is the base for every
|
||||
// per-operation timeout context, so a server shutdown cancels in-flight heal
|
||||
// restarts and update redeploys instead of letting them run detached.
|
||||
baseCtx context.Context
|
||||
|
||||
scheduler *scheduler.Scheduler
|
||||
dataStore dataservices.DataStore
|
||||
clientFactory *dockerclient.ClientFactory
|
||||
|
||||
// Dependencies used by the auto-update job (M4).
|
||||
digestClient *images.DigestClient
|
||||
containerService *docker.ContainerService
|
||||
stackDeployer deployments.StackDeployer
|
||||
|
||||
// notifier receives automation events (update/rollback/failure/heal). The
|
||||
// default is logNotifier; the field is the seam external senders plug into.
|
||||
notifier Notifier
|
||||
|
||||
mu sync.Mutex
|
||||
healJobID string
|
||||
updateJobID string
|
||||
|
||||
// running guards against overlapping heal ticks.
|
||||
running atomic.Bool
|
||||
// updateRunning guards against overlapping update ticks.
|
||||
updateRunning atomic.Bool
|
||||
|
||||
retryMu sync.Mutex
|
||||
retries map[string]retryState
|
||||
|
||||
// rolledBackMu guards rolledBack.
|
||||
rolledBackMu sync.Mutex
|
||||
// rolledBack records standalone containers whose update was rolled back, keyed
|
||||
// by endpoint+name, so the auto-update job does not immediately re-pull the
|
||||
// same failed image and roll back again on the next tick (the update->rollback
|
||||
// loop guard, mirroring the auto-heal retries map).
|
||||
//
|
||||
// This state is in-memory only and is NOT persisted: after a Portainer restart
|
||||
// the map is empty, so at most one extra update->rollback cycle per restart is
|
||||
// possible before the guard re-records the failed target. Persisting it would
|
||||
// require a datastore schema (key + digest + timestamp) and is intentionally out
|
||||
// of scope here; the cooldown-bounded single extra cycle is an acceptable
|
||||
// trade-off against that complexity.
|
||||
rolledBack map[string]rolledBackTarget
|
||||
}
|
||||
|
||||
// NewService creates a new container automation service. Call Start to schedule
|
||||
// the jobs according to the persisted settings. baseCtx is the application
|
||||
// shutdown context: it bounds the job operation contexts so a shutdown cancels
|
||||
// any in-flight heal/update. The stackDeployer and containerService are used by
|
||||
// the auto-update job; they may be nil only in tests that do not exercise
|
||||
// auto-update.
|
||||
func NewService(
|
||||
baseCtx context.Context,
|
||||
scheduler *scheduler.Scheduler,
|
||||
dataStore dataservices.DataStore,
|
||||
clientFactory *dockerclient.ClientFactory,
|
||||
containerService *docker.ContainerService,
|
||||
stackDeployer deployments.StackDeployer,
|
||||
) *Service {
|
||||
if baseCtx == nil {
|
||||
baseCtx = context.Background()
|
||||
}
|
||||
|
||||
return &Service{
|
||||
baseCtx: baseCtx,
|
||||
scheduler: scheduler,
|
||||
dataStore: dataStore,
|
||||
clientFactory: clientFactory,
|
||||
digestClient: images.NewClientWithRegistry(images.NewRegistryClient(dataStore), clientFactory),
|
||||
containerService: containerService,
|
||||
stackDeployer: stackDeployer,
|
||||
// Compose the always-on log notifier with the optional webhook notifier.
|
||||
// The webhook reads the current settings per-event from the datastore, so a
|
||||
// URL change in the UI takes effect without a restart; logNotifier keeps the
|
||||
// existing structured log output unchanged.
|
||||
notifier: multiNotifier{logNotifier{}, newWebhookNotifier(dataStore)},
|
||||
retries: make(map[string]retryState),
|
||||
rolledBack: make(map[string]rolledBackTarget),
|
||||
}
|
||||
}
|
||||
|
||||
// AutomationEnabledForEndpoint reports whether container automation (auto-heal and
|
||||
// auto-update) should run for an environment. It is the per-endpoint opt-out (M5)
|
||||
// layered on top of the global switch: an environment participates unless it has
|
||||
// been explicitly disabled. The zero value (not disabled) preserves the
|
||||
// pre-M5 behavior for every existing environment.
|
||||
func AutomationEnabledForEndpoint(endpoint *portainer.Endpoint) bool {
|
||||
return endpoint != nil && !endpoint.ContainerAutomationDisabled
|
||||
}
|
||||
|
||||
// Start schedules the enabled jobs according to the persisted settings.
|
||||
func (s *Service) Start() {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
s.start()
|
||||
}
|
||||
|
||||
// Reload re-applies the current settings: it stops the running jobs and starts
|
||||
// fresh ones with the new intervals, or leaves them stopped if disabled. It is
|
||||
// safe to call after a settings update.
|
||||
//
|
||||
// Note: stopping a job unschedules future ticks but does not interrupt a tick
|
||||
// already in progress. An in-flight heal/update pass runs to completion on its
|
||||
// original (pre-reload) context and is only cancelled by a server shutdown (via
|
||||
// baseCtx); the new interval takes effect from the next scheduled tick. The
|
||||
// overlap guards (running/updateRunning) and the per-map mutexes keep this safe
|
||||
// against data races, so this is a deliberate behavioural nuance, not a bug.
|
||||
func (s *Service) Reload() error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
s.stop()
|
||||
s.start()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// start (re)schedules the enabled jobs from settings. Caller must hold s.mu.
|
||||
func (s *Service) start() {
|
||||
settings, err := s.dataStore.Settings().Settings()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("container automation: unable to read settings, jobs not scheduled")
|
||||
return
|
||||
}
|
||||
|
||||
s.startHeal(settings)
|
||||
s.startUpdate(settings)
|
||||
}
|
||||
|
||||
// startHeal schedules the auto-heal job if enabled. Caller must hold s.mu.
|
||||
func (s *Service) startHeal(settings *portainer.Settings) {
|
||||
if s.healJobID != "" {
|
||||
return
|
||||
}
|
||||
|
||||
autoHeal := settings.ContainerAutomation.AutoHeal
|
||||
if !autoHeal.Enabled {
|
||||
return
|
||||
}
|
||||
|
||||
interval, err := time.ParseDuration(autoHeal.CheckInterval)
|
||||
if err != nil || interval <= 0 {
|
||||
log.Warn().Str("interval", autoHeal.CheckInterval).Dur("default", defaultCheckInterval).
|
||||
Msg("auto-heal: invalid check interval, falling back to default")
|
||||
interval = defaultCheckInterval
|
||||
}
|
||||
|
||||
s.healJobID = s.scheduler.StartJobEvery(interval, s.heal)
|
||||
log.Info().Dur("interval", interval).Msg("auto-heal: job scheduled")
|
||||
}
|
||||
|
||||
// startUpdate schedules the auto-update job if enabled. Caller must hold s.mu.
|
||||
func (s *Service) startUpdate(settings *portainer.Settings) {
|
||||
if s.updateJobID != "" {
|
||||
return
|
||||
}
|
||||
|
||||
autoUpdate := settings.ContainerAutomation.AutoUpdate
|
||||
if !autoUpdate.Enabled {
|
||||
return
|
||||
}
|
||||
|
||||
interval, err := time.ParseDuration(autoUpdate.PollInterval)
|
||||
if err != nil || interval <= 0 {
|
||||
log.Warn().Str("interval", autoUpdate.PollInterval).Dur("default", defaultPollInterval).
|
||||
Msg("auto-update: invalid poll interval, falling back to default")
|
||||
interval = defaultPollInterval
|
||||
}
|
||||
|
||||
s.updateJobID = s.scheduler.StartJobEvery(interval, s.update)
|
||||
log.Info().Dur("interval", interval).Msg("auto-update: job scheduled")
|
||||
}
|
||||
|
||||
// stop cancels the running jobs, if any. Caller must hold s.mu.
|
||||
func (s *Service) stop() {
|
||||
if s.healJobID != "" {
|
||||
if err := s.scheduler.StopJob(s.healJobID); err != nil {
|
||||
log.Warn().Err(err).Msg("auto-heal: could not stop the job")
|
||||
}
|
||||
|
||||
s.healJobID = ""
|
||||
}
|
||||
|
||||
if s.updateJobID != "" {
|
||||
if err := s.scheduler.StopJob(s.updateJobID); err != nil {
|
||||
log.Warn().Err(err).Msg("auto-update: could not stop the job")
|
||||
}
|
||||
|
||||
s.updateJobID = ""
|
||||
}
|
||||
}
|
||||
|
||||
// scope returns the configured auto-heal scope, defaulting to "labeled".
|
||||
func (s *Service) scope() string {
|
||||
settings, err := s.dataStore.Settings().Settings()
|
||||
if err != nil {
|
||||
return ScopeLabeled
|
||||
}
|
||||
|
||||
if settings.ContainerAutomation.AutoHeal.Scope == ScopeAll {
|
||||
return ScopeAll
|
||||
}
|
||||
|
||||
return ScopeLabeled
|
||||
}
|
||||
|
||||
// getRetry returns the retry state for a container (zero value if unknown).
|
||||
func (s *Service) getRetry(containerID string) retryState {
|
||||
s.retryMu.Lock()
|
||||
defer s.retryMu.Unlock()
|
||||
|
||||
return s.retries[containerID]
|
||||
}
|
||||
|
||||
// setRetry stores the retry state for a container.
|
||||
func (s *Service) setRetry(containerID string, state retryState) {
|
||||
s.retryMu.Lock()
|
||||
defer s.retryMu.Unlock()
|
||||
|
||||
s.retries[containerID] = state
|
||||
}
|
||||
|
||||
// getRolledBack returns the rolled-back target for a key and whether it exists.
|
||||
func (s *Service) getRolledBack(key string) (rolledBackTarget, bool) {
|
||||
s.rolledBackMu.Lock()
|
||||
defer s.rolledBackMu.Unlock()
|
||||
|
||||
rec, ok := s.rolledBack[key]
|
||||
|
||||
return rec, ok
|
||||
}
|
||||
|
||||
// setRolledBack records a rolled-back target for a key.
|
||||
func (s *Service) setRolledBack(key string, rec rolledBackTarget) {
|
||||
s.rolledBackMu.Lock()
|
||||
defer s.rolledBackMu.Unlock()
|
||||
|
||||
s.rolledBack[key] = rec
|
||||
}
|
||||
|
||||
// clearRolledBack drops the rolled-back record for a key (cooldown elapsed or a
|
||||
// new upstream image lifted the skip).
|
||||
func (s *Service) clearRolledBack(key string) {
|
||||
s.rolledBackMu.Lock()
|
||||
defer s.rolledBackMu.Unlock()
|
||||
|
||||
delete(s.rolledBack, key)
|
||||
}
|
||||
|
||||
// pruneRolledBack drops rolled-back records whose cooldown has fully elapsed, so
|
||||
// the map cannot grow unbounded. It mirrors pruneRetries.
|
||||
func (s *Service) pruneRolledBack(now time.Time) {
|
||||
s.rolledBackMu.Lock()
|
||||
defer s.rolledBackMu.Unlock()
|
||||
|
||||
for key, rec := range s.rolledBack {
|
||||
if now.Sub(rec.at) >= updateRollbackCooldown {
|
||||
delete(s.rolledBack, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// pruneRetries drops retry state for containers whose retry window has fully
|
||||
// elapsed since their last restart. A container is kept regardless of whether it
|
||||
// appeared in the current tick: one that briefly leaves the unhealthy filter
|
||||
// (e.g. while "starting" right after a restart) must not lose its accounting, or
|
||||
// the cooldown / max-retries storm guard would be defeated. A container that has
|
||||
// recovered and stayed quiet for longer than the window is cleaned up (fresh
|
||||
// budget next incident, no unbounded growth).
|
||||
func (s *Service) pruneRetries(now time.Time) {
|
||||
s.retryMu.Lock()
|
||||
defer s.retryMu.Unlock()
|
||||
|
||||
for id, state := range s.retries {
|
||||
if now.Sub(state.lastRestart) >= retryWindow {
|
||||
delete(s.retries, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,261 +0,0 @@
|
||||
package containerautomation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/dataservices"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
const (
|
||||
// webhookMessagePlaceholder is the token replaced in the configured webhook
|
||||
// URL with the URL-encoded event message. When present, the notifier issues a
|
||||
// GET on the substituted URL ("message in the address"); when absent, it POSTs
|
||||
// the plain-text message as the request body.
|
||||
webhookMessagePlaceholder = "{{message}}"
|
||||
|
||||
// webhookTimeout bounds each webhook HTTP call so a slow or unresponsive
|
||||
// endpoint cannot pile up goroutines. The call already runs off the hot path.
|
||||
webhookTimeout = 10 * time.Second
|
||||
|
||||
// shortDigestLen is how many leading hex characters of an image digest the
|
||||
// message keeps (matches the maintainer's example, e.g. "59b94983c73a").
|
||||
shortDigestLen = 12
|
||||
)
|
||||
|
||||
// webhookNotifier delivers container-automation events to a user-configured HTTP
|
||||
// endpoint. It reads the current webhook URL from the datastore on every event
|
||||
// so a settings change takes effect without a restart, formats a human-readable
|
||||
// message, and performs the HTTP call in a background goroutine so a slow or
|
||||
// broken endpoint never delays or fails the daemon hot path.
|
||||
type webhookNotifier struct {
|
||||
dataStore dataservices.DataStore
|
||||
client *http.Client
|
||||
}
|
||||
|
||||
// newWebhookNotifier builds a webhookNotifier bound to the datastore. The HTTP
|
||||
// client carries the per-call timeout so a request cannot hang indefinitely.
|
||||
func newWebhookNotifier(dataStore dataservices.DataStore) webhookNotifier {
|
||||
return webhookNotifier{
|
||||
dataStore: dataStore,
|
||||
client: &http.Client{Timeout: webhookTimeout},
|
||||
}
|
||||
}
|
||||
|
||||
// webhookURLForKind selects the configured webhook URL for an event kind: the
|
||||
// update-family events (image update, rollback, update-failed) route to the
|
||||
// update endpoint, and the auto-heal restart routes to the heal endpoint. This
|
||||
// lets a user enable notifications for one mechanism without the other — an
|
||||
// empty URL for a mechanism means "no webhook for that mechanism".
|
||||
func webhookURLForKind(notification portainer.ContainerAutomationNotificationSettings, kind EventKind) string {
|
||||
switch kind {
|
||||
case EventUpdated, EventRollback, EventUpdateFailed:
|
||||
return notification.UpdateWebhookURL
|
||||
case EventHealRestarted:
|
||||
return notification.HealWebhookURL
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
// Notify reads the webhook URL for the event's mechanism (update vs heal) and,
|
||||
// when set, dispatches the event in a background goroutine. Only the settings
|
||||
// read and the empty-URL short-circuit run synchronously (they decide whether
|
||||
// to spawn at all); message formatting — which itself reads Endpoint()/Stack()
|
||||
// from the datastore — and the HTTP call both happen off the daemon hot path,
|
||||
// under a single recover(). It never blocks the caller and never returns an
|
||||
// error: the webhook is strictly best-effort. When the URL for the event's
|
||||
// mechanism is empty, the event is skipped and the other mechanism is
|
||||
// unaffected.
|
||||
func (n webhookNotifier) Notify(event Event) {
|
||||
settings, err := n.dataStore.Settings().Settings()
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("container automation webhook: unable to read settings, skipping notification")
|
||||
return
|
||||
}
|
||||
|
||||
webhookURL := strings.TrimSpace(webhookURLForKind(settings.ContainerAutomation.Notification, event.Kind))
|
||||
if webhookURL == "" {
|
||||
return
|
||||
}
|
||||
|
||||
// Best-effort delivery: never block or fail the caller (the update/heal hot
|
||||
// path). Everything below — the env/stack datastore reads in formatMessage and
|
||||
// the bounded HTTP call — runs in its own goroutine, and any panic there is
|
||||
// recovered so it can never crash the daemon.
|
||||
go func() {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Warn().Interface("panic", r).Msg("container automation webhook: recovered from panic during delivery")
|
||||
}
|
||||
}()
|
||||
|
||||
message := n.formatMessage(settings, event)
|
||||
n.deliver(webhookURL, message)
|
||||
}()
|
||||
}
|
||||
|
||||
// deliver performs the HTTP call for a single event. It is always invoked from
|
||||
// the Notify goroutine (which recovers any panic), so a broken endpoint can
|
||||
// never block or crash the daemon.
|
||||
func (n webhookNotifier) deliver(webhookURL, message string) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), webhookTimeout)
|
||||
defer cancel()
|
||||
|
||||
var (
|
||||
req *http.Request
|
||||
err error
|
||||
)
|
||||
|
||||
if strings.Contains(webhookURL, webhookMessagePlaceholder) {
|
||||
// Substitution mode: replace the placeholder with the URL-encoded message
|
||||
// and GET the resulting address (the maintainer's "message in the URL").
|
||||
target := strings.ReplaceAll(webhookURL, webhookMessagePlaceholder, url.QueryEscape(message))
|
||||
req, err = http.NewRequestWithContext(ctx, http.MethodGet, target, nil)
|
||||
} else {
|
||||
// No placeholder: POST the plain-text message as the body, useful for
|
||||
// generic POST-style webhooks.
|
||||
req, err = http.NewRequestWithContext(ctx, http.MethodPost, webhookURL, strings.NewReader(message))
|
||||
if err == nil {
|
||||
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("container automation webhook: unable to build request")
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := n.client.Do(req)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("container automation webhook: delivery failed")
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode >= http.StatusBadRequest {
|
||||
log.Warn().Int("status", resp.StatusCode).Msg("container automation webhook: endpoint returned an error status")
|
||||
}
|
||||
}
|
||||
|
||||
// formatMessage builds the human-readable message for an event. It resolves the
|
||||
// environment name from the endpoint and the stack name from the stack via the
|
||||
// datastore, mirroring the maintainer's example:
|
||||
//
|
||||
// Environment | nebula.lc
|
||||
// Stack [cache-demo]
|
||||
// Update [esphome]: 59b94983c73a → 2231ca5d676d
|
||||
//
|
||||
// The context line is the stack for stack-scoped events, otherwise the container;
|
||||
// the action line is adapted per event kind (update / rollback / update-failed /
|
||||
// auto-heal restart). Auto-heal renders as:
|
||||
//
|
||||
// Environment | nebula.lc
|
||||
// Container [nginx]
|
||||
// Auto-heal: restarted unhealthy container
|
||||
func (n webhookNotifier) formatMessage(settings *portainer.Settings, event Event) string {
|
||||
lines := []string{"Environment | " + n.environmentName(event.EndpointID)}
|
||||
|
||||
// Context line: the stack for stack-scoped events, otherwise the container. A
|
||||
// per-container stack-member update carries StackName (from the compose label),
|
||||
// preferred over a StackID/Stack().Read round-trip; the container itself still
|
||||
// names the action line below.
|
||||
switch {
|
||||
case event.StackName != "":
|
||||
lines = append(lines, fmt.Sprintf("Stack [%s]", event.StackName))
|
||||
case event.StackID != 0:
|
||||
lines = append(lines, fmt.Sprintf("Stack [%s]", n.stackName(event.StackID)))
|
||||
case event.ContainerName != "":
|
||||
lines = append(lines, fmt.Sprintf("Container [%s]", event.ContainerName))
|
||||
}
|
||||
|
||||
// Subject for the action line: the container name when known, else the stack
|
||||
// name, else a short container id.
|
||||
subject := event.ContainerName
|
||||
if subject == "" && event.StackID != 0 {
|
||||
subject = n.stackName(event.StackID)
|
||||
}
|
||||
if subject == "" {
|
||||
subject = shortDigest(event.ContainerID)
|
||||
}
|
||||
|
||||
switch event.Kind {
|
||||
case EventUpdated:
|
||||
if event.OldDigest != "" && event.NewDigest != "" {
|
||||
lines = append(lines, fmt.Sprintf("Update [%s]: %s → %s", subject, shortDigest(event.OldDigest), shortDigest(event.NewDigest)))
|
||||
} else {
|
||||
lines = append(lines, fmt.Sprintf("Update [%s]: image updated", subject))
|
||||
}
|
||||
case EventRollback:
|
||||
lines = append(lines, fmt.Sprintf("Rollback [%s]: rolled back to previous image after failed health check", subject))
|
||||
case EventUpdateFailed:
|
||||
line := fmt.Sprintf("Update failed [%s]", subject)
|
||||
if event.Message != "" {
|
||||
line += ": " + event.Message
|
||||
}
|
||||
if event.Err != nil {
|
||||
line += fmt.Sprintf(" (%s)", event.Err)
|
||||
}
|
||||
lines = append(lines, line)
|
||||
case EventHealRestarted:
|
||||
lines = append(lines, "Auto-heal: restarted unhealthy container")
|
||||
default:
|
||||
if event.Message != "" {
|
||||
lines = append(lines, event.Message)
|
||||
}
|
||||
}
|
||||
|
||||
return strings.Join(lines, "\n")
|
||||
}
|
||||
|
||||
// environmentName resolves an endpoint id to its display name, degrading to a
|
||||
// "#<id>" placeholder when the endpoint cannot be read (deleted, or a zero id).
|
||||
func (n webhookNotifier) environmentName(endpointID int) string {
|
||||
if endpointID == 0 {
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
endpoint, err := n.dataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID))
|
||||
if err != nil || endpoint == nil {
|
||||
return fmt.Sprintf("#%d", endpointID)
|
||||
}
|
||||
|
||||
return endpoint.Name
|
||||
}
|
||||
|
||||
// stackName resolves a stack id to its name, degrading to a "#<id>" placeholder
|
||||
// when the stack cannot be read.
|
||||
func (n webhookNotifier) stackName(stackID int) string {
|
||||
stack, err := n.dataStore.Stack().Read(portainer.StackID(stackID))
|
||||
if err != nil || stack == nil {
|
||||
return fmt.Sprintf("#%d", stackID)
|
||||
}
|
||||
|
||||
return stack.Name
|
||||
}
|
||||
|
||||
// shortDigest trims an image id/digest to a short, human-friendly hex form
|
||||
// (shortDigestLen chars), matching the maintainer's example. It drops a leading
|
||||
// "sha256:" algorithm prefix so "sha256:59b94983c73a..." -> "59b94983c73a".
|
||||
func shortDigest(s string) string {
|
||||
if s == "" {
|
||||
return ""
|
||||
}
|
||||
|
||||
if i := strings.LastIndex(s, "sha256:"); i >= 0 {
|
||||
s = s[i+len("sha256:"):]
|
||||
}
|
||||
|
||||
if len(s) > shortDigestLen {
|
||||
return s[:shortDigestLen]
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
@@ -1,437 +0,0 @@
|
||||
package containerautomation
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/datastore"
|
||||
)
|
||||
|
||||
// newTestWebhookNotifier builds an initialized test datastore, sets both the
|
||||
// update and heal webhook URLs to the same value (so the notifier fires for
|
||||
// every event kind), and returns a webhookNotifier bound to it. Use
|
||||
// newTestWebhookNotifierSplit to configure the two URLs independently.
|
||||
func newTestWebhookNotifier(t *testing.T, webhookURL string) (webhookNotifier, *datastore.Store) {
|
||||
t.Helper()
|
||||
|
||||
return newTestWebhookNotifierSplit(t, webhookURL, webhookURL)
|
||||
}
|
||||
|
||||
// newTestWebhookNotifierSplit builds an initialized test datastore with the
|
||||
// auto-update and auto-heal webhook URLs set independently, and returns a
|
||||
// webhookNotifier bound to it.
|
||||
func newTestWebhookNotifierSplit(t *testing.T, updateURL, healURL string) (webhookNotifier, *datastore.Store) {
|
||||
t.Helper()
|
||||
|
||||
_, store := datastore.MustNewTestStore(t, true, false)
|
||||
|
||||
settings, err := store.Settings().Settings()
|
||||
if err != nil {
|
||||
t.Fatalf("read settings: %v", err)
|
||||
}
|
||||
|
||||
settings.ContainerAutomation.Notification.UpdateWebhookURL = updateURL
|
||||
settings.ContainerAutomation.Notification.HealWebhookURL = healURL
|
||||
if err := store.Settings().UpdateSettings(settings); err != nil {
|
||||
t.Fatalf("update settings: %v", err)
|
||||
}
|
||||
|
||||
return newWebhookNotifier(store), store
|
||||
}
|
||||
|
||||
func createEndpoint(t *testing.T, store *datastore.Store, id int, name string) {
|
||||
t.Helper()
|
||||
|
||||
if err := store.Endpoint().Create(&portainer.Endpoint{ID: portainer.EndpointID(id), Name: name}); err != nil {
|
||||
t.Fatalf("create endpoint: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func createStack(t *testing.T, store *datastore.Store, id int, name string) {
|
||||
t.Helper()
|
||||
|
||||
if err := store.Stack().Create(&portainer.Stack{ID: portainer.StackID(id), Name: name}); err != nil {
|
||||
t.Fatalf("create stack: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWebhookNotifierGETPlaceholder verifies the placeholder is replaced with the
|
||||
// URL-encoded message and the URL is fetched with GET.
|
||||
func TestWebhookNotifierGETPlaceholder(t *testing.T) {
|
||||
reqs := make(chan *http.Request, 1)
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
reqs <- r
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
n, store := newTestWebhookNotifier(t, srv.URL+"/hook?msg="+webhookMessagePlaceholder)
|
||||
createEndpoint(t, store, 1, "prod")
|
||||
|
||||
n.Notify(Event{Kind: EventHealRestarted, EndpointID: 1, ContainerName: "nginx"})
|
||||
|
||||
select {
|
||||
case r := <-reqs:
|
||||
if r.Method != http.MethodGet {
|
||||
t.Errorf("method = %s, want GET", r.Method)
|
||||
}
|
||||
|
||||
got := r.URL.Query().Get("msg")
|
||||
want := "Environment | prod\nContainer [nginx]\nAuto-heal: restarted unhealthy container"
|
||||
if got != want {
|
||||
t.Errorf("decoded msg = %q, want %q", got, want)
|
||||
}
|
||||
|
||||
// The raw query must be URL-encoded: no literal spaces/newlines on the wire.
|
||||
if strings.ContainsAny(r.URL.RawQuery, " \n") {
|
||||
t.Errorf("raw query is not URL-encoded: %q", r.URL.RawQuery)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("webhook GET was not received")
|
||||
}
|
||||
}
|
||||
|
||||
// TestWebhookNotifierPOSTFallback verifies that a URL without the placeholder is
|
||||
// POSTed with the plain-text message as the body.
|
||||
func TestWebhookNotifierPOSTFallback(t *testing.T) {
|
||||
type captured struct {
|
||||
method string
|
||||
body string
|
||||
}
|
||||
|
||||
ch := make(chan captured, 1)
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
ch <- captured{method: r.Method, body: string(b)}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
n, store := newTestWebhookNotifier(t, srv.URL+"/hook")
|
||||
createEndpoint(t, store, 2, "staging")
|
||||
|
||||
n.Notify(Event{Kind: EventHealRestarted, EndpointID: 2, ContainerName: "api"})
|
||||
|
||||
select {
|
||||
case c := <-ch:
|
||||
if c.method != http.MethodPost {
|
||||
t.Errorf("method = %s, want POST", c.method)
|
||||
}
|
||||
|
||||
want := "Environment | staging\nContainer [api]\nAuto-heal: restarted unhealthy container"
|
||||
if c.body != want {
|
||||
t.Errorf("body = %q, want %q", c.body, want)
|
||||
}
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("webhook POST was not received")
|
||||
}
|
||||
}
|
||||
|
||||
// TestWebhookNotifierEmptyURLNoCall verifies no HTTP call is made when the URL is
|
||||
// empty.
|
||||
func TestWebhookNotifierEmptyURLNoCall(t *testing.T) {
|
||||
called := make(chan struct{}, 1)
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
called <- struct{}{}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
n, _ := newTestWebhookNotifier(t, "")
|
||||
n.Notify(Event{Kind: EventHealRestarted, EndpointID: 1, ContainerName: "x"})
|
||||
|
||||
select {
|
||||
case <-called:
|
||||
t.Fatal("webhook should not be called when the URL is empty")
|
||||
case <-time.After(300 * time.Millisecond):
|
||||
// No call, as expected.
|
||||
}
|
||||
}
|
||||
|
||||
// waitForRequest returns the first request seen on ch, or fails after a short
|
||||
// grace period.
|
||||
func waitForRequest(t *testing.T, ch <-chan *http.Request, what string) *http.Request {
|
||||
t.Helper()
|
||||
|
||||
select {
|
||||
case r := <-ch:
|
||||
return r
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatalf("%s was not received", what)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// expectNoRequest asserts nothing arrives on ch within a short grace period.
|
||||
func expectNoRequest(t *testing.T, ch <-chan *http.Request, what string) {
|
||||
t.Helper()
|
||||
|
||||
select {
|
||||
case <-ch:
|
||||
t.Fatalf("%s should not have been called", what)
|
||||
case <-time.After(300 * time.Millisecond):
|
||||
// No call, as expected.
|
||||
}
|
||||
}
|
||||
|
||||
// TestWebhookNotifierUpdateEventRoutesToUpdateURL verifies an update-family event
|
||||
// dispatches to the auto-update URL only; the heal URL is set but never called.
|
||||
func TestWebhookNotifierUpdateEventRoutesToUpdateURL(t *testing.T) {
|
||||
updateReqs := make(chan *http.Request, 1)
|
||||
updateSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
updateReqs <- r
|
||||
}))
|
||||
defer updateSrv.Close()
|
||||
|
||||
healReqs := make(chan *http.Request, 1)
|
||||
healSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
healReqs <- r
|
||||
}))
|
||||
defer healSrv.Close()
|
||||
|
||||
n, store := newTestWebhookNotifierSplit(t, updateSrv.URL+"/update", healSrv.URL+"/heal")
|
||||
createEndpoint(t, store, 1, "prod")
|
||||
|
||||
for _, kind := range []EventKind{EventUpdated, EventRollback, EventUpdateFailed} {
|
||||
n.Notify(Event{Kind: kind, EndpointID: 1, ContainerName: "c"})
|
||||
|
||||
r := waitForRequest(t, updateReqs, "update webhook for "+string(kind))
|
||||
if r.URL.Path != "/update" {
|
||||
t.Errorf("kind %s hit %q, want /update", kind, r.URL.Path)
|
||||
}
|
||||
}
|
||||
|
||||
expectNoRequest(t, healReqs, "heal webhook")
|
||||
}
|
||||
|
||||
// TestWebhookNotifierHealEventRoutesToHealURL verifies a heal event dispatches to
|
||||
// the auto-heal URL only; the update URL is set but never called.
|
||||
func TestWebhookNotifierHealEventRoutesToHealURL(t *testing.T) {
|
||||
updateReqs := make(chan *http.Request, 1)
|
||||
updateSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
updateReqs <- r
|
||||
}))
|
||||
defer updateSrv.Close()
|
||||
|
||||
healReqs := make(chan *http.Request, 1)
|
||||
healSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
healReqs <- r
|
||||
}))
|
||||
defer healSrv.Close()
|
||||
|
||||
n, store := newTestWebhookNotifierSplit(t, updateSrv.URL+"/update", healSrv.URL+"/heal")
|
||||
createEndpoint(t, store, 1, "prod")
|
||||
|
||||
n.Notify(Event{Kind: EventHealRestarted, EndpointID: 1, ContainerName: "nginx"})
|
||||
|
||||
r := waitForRequest(t, healReqs, "heal webhook")
|
||||
if r.URL.Path != "/heal" {
|
||||
t.Errorf("heal event hit %q, want /heal", r.URL.Path)
|
||||
}
|
||||
|
||||
expectNoRequest(t, updateReqs, "update webhook")
|
||||
}
|
||||
|
||||
// TestWebhookNotifierEmptyUpdateURLSkipsUpdateOnly verifies that an empty
|
||||
// auto-update URL suppresses update-family events while heal still fires.
|
||||
func TestWebhookNotifierEmptyUpdateURLSkipsUpdateOnly(t *testing.T) {
|
||||
healReqs := make(chan *http.Request, 1)
|
||||
healSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
healReqs <- r
|
||||
}))
|
||||
defer healSrv.Close()
|
||||
|
||||
n, store := newTestWebhookNotifierSplit(t, "", healSrv.URL+"/heal")
|
||||
createEndpoint(t, store, 1, "prod")
|
||||
|
||||
// Update-family event: no URL configured, so nothing is delivered.
|
||||
n.Notify(Event{Kind: EventUpdated, EndpointID: 1, ContainerName: "c"})
|
||||
expectNoRequest(t, healReqs, "heal webhook on an update event")
|
||||
|
||||
// Heal event: the heal URL is set, so it still fires.
|
||||
n.Notify(Event{Kind: EventHealRestarted, EndpointID: 1, ContainerName: "nginx"})
|
||||
waitForRequest(t, healReqs, "heal webhook")
|
||||
}
|
||||
|
||||
// TestWebhookNotifierEmptyHealURLSkipsHealOnly verifies that an empty auto-heal
|
||||
// URL suppresses heal events while update-family events still fire.
|
||||
func TestWebhookNotifierEmptyHealURLSkipsHealOnly(t *testing.T) {
|
||||
updateReqs := make(chan *http.Request, 1)
|
||||
updateSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
updateReqs <- r
|
||||
}))
|
||||
defer updateSrv.Close()
|
||||
|
||||
n, store := newTestWebhookNotifierSplit(t, updateSrv.URL+"/update", "")
|
||||
createEndpoint(t, store, 1, "prod")
|
||||
|
||||
// Heal event: no URL configured, so nothing is delivered.
|
||||
n.Notify(Event{Kind: EventHealRestarted, EndpointID: 1, ContainerName: "nginx"})
|
||||
expectNoRequest(t, updateReqs, "update webhook on a heal event")
|
||||
|
||||
// Update event: the update URL is set, so it still fires.
|
||||
n.Notify(Event{Kind: EventUpdated, EndpointID: 1, ContainerName: "c"})
|
||||
waitForRequest(t, updateReqs, "update webhook")
|
||||
}
|
||||
|
||||
// TestWebhookNotifierFailingEndpointDoesNotBlock verifies that a broken endpoint
|
||||
// neither blocks the caller nor panics.
|
||||
func TestWebhookNotifierFailingEndpointDoesNotBlock(t *testing.T) {
|
||||
// Start then immediately close a server so its address refuses connections.
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
||||
deadURL := srv.URL
|
||||
srv.Close()
|
||||
|
||||
n, store := newTestWebhookNotifier(t, deadURL+"/hook?msg="+webhookMessagePlaceholder)
|
||||
createEndpoint(t, store, 1, "prod")
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
n.Notify(Event{Kind: EventUpdated, EndpointID: 1, ContainerName: "c"})
|
||||
close(done)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
// Notify returned promptly despite the failing endpoint.
|
||||
case <-time.After(1 * time.Second):
|
||||
t.Fatal("Notify blocked on a failing endpoint")
|
||||
}
|
||||
|
||||
// Give the background delivery goroutine time to hit the error path; it must
|
||||
// log-and-return, never panic.
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
|
||||
// TestFormatMessageStandaloneUpdate covers the maintainer's update format for a
|
||||
// standalone container, with the old->new short digests.
|
||||
func TestFormatMessageStandaloneUpdate(t *testing.T) {
|
||||
n, store := newTestWebhookNotifier(t, "unused")
|
||||
createEndpoint(t, store, 1, "nebula.lc")
|
||||
|
||||
settings, _ := store.Settings().Settings()
|
||||
|
||||
msg := n.formatMessage(settings, Event{
|
||||
Kind: EventUpdated, EndpointID: 1, ContainerName: "esphome",
|
||||
OldDigest: "sha256:59b94983c73aabcd", NewDigest: "sha256:2231ca5d676dabcd",
|
||||
})
|
||||
|
||||
want := "Environment | nebula.lc\nContainer [esphome]\nUpdate [esphome]: 59b94983c73a → 2231ca5d676d"
|
||||
if msg != want {
|
||||
t.Errorf("got:\n%q\nwant:\n%q", msg, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFormatMessageStackUpdate covers a stack-scoped update (no per-container
|
||||
// digests): the context line is the stack name.
|
||||
func TestFormatMessageStackUpdate(t *testing.T) {
|
||||
n, store := newTestWebhookNotifier(t, "unused")
|
||||
createEndpoint(t, store, 1, "nebula.lc")
|
||||
createStack(t, store, 7, "cache-demo")
|
||||
|
||||
settings, _ := store.Settings().Settings()
|
||||
|
||||
msg := n.formatMessage(settings, Event{
|
||||
Kind: EventUpdated, EndpointID: 1, StackID: 7,
|
||||
})
|
||||
|
||||
want := "Environment | nebula.lc\nStack [cache-demo]\nUpdate [cache-demo]: image updated"
|
||||
if msg != want {
|
||||
t.Errorf("got:\n%q\nwant:\n%q", msg, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFormatMessageStackMemberUpdate covers the per-container update of a
|
||||
// stack-member container: the context line is the compose stack name (from
|
||||
// StackName, no Stack().Read), the action line names the container with its
|
||||
// old->new digests. This is the maintainer's target output.
|
||||
func TestFormatMessageStackMemberUpdate(t *testing.T) {
|
||||
n, store := newTestWebhookNotifier(t, "unused")
|
||||
createEndpoint(t, store, 1, "nebula.lc")
|
||||
|
||||
settings, _ := store.Settings().Settings()
|
||||
|
||||
msg := n.formatMessage(settings, Event{
|
||||
Kind: EventUpdated, EndpointID: 1, StackID: 7, StackName: "cache-demo",
|
||||
ContainerName: "esphome",
|
||||
OldDigest: "sha256:59b94983c73aabcd", NewDigest: "sha256:2231ca5d676dabcd",
|
||||
})
|
||||
|
||||
want := "Environment | nebula.lc\nStack [cache-demo]\nUpdate [esphome]: 59b94983c73a → 2231ca5d676d"
|
||||
if msg != want {
|
||||
t.Errorf("got:\n%q\nwant:\n%q", msg, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFormatMessageStackMemberUpdateNoNewDigest covers the best-effort fallback:
|
||||
// when the post-redeploy new image id could not be recovered, the message still
|
||||
// carries the stack and container and degrades the action line to "image updated"
|
||||
// rather than blocking delivery.
|
||||
func TestFormatMessageStackMemberUpdateNoNewDigest(t *testing.T) {
|
||||
n, store := newTestWebhookNotifier(t, "unused")
|
||||
createEndpoint(t, store, 1, "nebula.lc")
|
||||
|
||||
settings, _ := store.Settings().Settings()
|
||||
|
||||
msg := n.formatMessage(settings, Event{
|
||||
Kind: EventUpdated, EndpointID: 1, StackID: 7, StackName: "cache-demo",
|
||||
ContainerName: "esphome", OldDigest: "sha256:59b94983c73aabcd",
|
||||
})
|
||||
|
||||
want := "Environment | nebula.lc\nStack [cache-demo]\nUpdate [esphome]: image updated"
|
||||
if msg != want {
|
||||
t.Errorf("got:\n%q\nwant:\n%q", msg, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFormatMessageAutoHeal covers the auto-heal message design.
|
||||
func TestFormatMessageAutoHeal(t *testing.T) {
|
||||
n, store := newTestWebhookNotifier(t, "unused")
|
||||
createEndpoint(t, store, 3, "prod")
|
||||
|
||||
settings, _ := store.Settings().Settings()
|
||||
|
||||
msg := n.formatMessage(settings, Event{
|
||||
Kind: EventHealRestarted, EndpointID: 3, ContainerName: "nginx",
|
||||
})
|
||||
|
||||
want := "Environment | prod\nContainer [nginx]\nAuto-heal: restarted unhealthy container"
|
||||
if msg != want {
|
||||
t.Errorf("got:\n%q\nwant:\n%q", msg, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestFormatMessageUnknownEndpoint verifies the "#<id>" fallback when the
|
||||
// endpoint cannot be resolved.
|
||||
func TestFormatMessageUnknownEndpoint(t *testing.T) {
|
||||
n, store := newTestWebhookNotifier(t, "unused")
|
||||
|
||||
settings, _ := store.Settings().Settings()
|
||||
|
||||
msg := n.formatMessage(settings, Event{
|
||||
Kind: EventHealRestarted, EndpointID: 99, ContainerName: "ghost",
|
||||
})
|
||||
|
||||
want := "Environment | #99\nContainer [ghost]\nAuto-heal: restarted unhealthy container"
|
||||
if msg != want {
|
||||
t.Errorf("got:\n%q\nwant:\n%q", msg, want)
|
||||
}
|
||||
}
|
||||
|
||||
// TestShortDigest covers digest short-forming.
|
||||
func TestShortDigest(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"sha256:59b94983c73a1122334455": "59b94983c73a",
|
||||
"59b94983c73a1122334455": "59b94983c73a",
|
||||
"short": "short",
|
||||
"": "",
|
||||
}
|
||||
|
||||
for in, want := range cases {
|
||||
if got := shortDigest(in); got != want {
|
||||
t.Errorf("shortDigest(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -164,9 +164,7 @@ func aesEncryptGCM(input io.Reader, output io.Writer, passphrase []byte) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := nonce.Increment(); err != nil {
|
||||
return err
|
||||
}
|
||||
nonce.Increment()
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -237,9 +235,7 @@ func aesDecryptGCM(input io.Reader, passphrase []byte) (io.Reader, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := nonce.Increment(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nonce.Increment()
|
||||
}
|
||||
|
||||
return &buf, nil
|
||||
|
||||
@@ -6,10 +6,9 @@ import (
|
||||
"io"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/portainer/portainer/api/filesystem"
|
||||
"github.com/portainer/portainer/api/logs"
|
||||
"github.com/portainer/portainer/pkg/fips"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -42,23 +41,22 @@ func Test_encryptAndDecrypt_withTheSamePassword(t *testing.T) {
|
||||
tmpdir := t.TempDir()
|
||||
|
||||
var (
|
||||
originFilePath = filesystem.JoinPaths(tmpdir, "origin")
|
||||
encryptedFilePath = filesystem.JoinPaths(tmpdir, "encrypted")
|
||||
decryptedFilePath = filesystem.JoinPaths(tmpdir, "decrypted")
|
||||
originFilePath = filepath.Join(tmpdir, "origin")
|
||||
encryptedFilePath = filepath.Join(tmpdir, "encrypted")
|
||||
decryptedFilePath = filepath.Join(tmpdir, "decrypted")
|
||||
)
|
||||
|
||||
content := randBytes(1024*1024*100 + 523)
|
||||
err := os.WriteFile(originFilePath, content, 0600)
|
||||
require.NoError(t, err)
|
||||
os.WriteFile(originFilePath, content, 0600)
|
||||
|
||||
originFile, _ := os.Open(originFilePath)
|
||||
defer logs.CloseAndLogErr(originFile)
|
||||
defer originFile.Close()
|
||||
|
||||
encryptedFileWriter, _ := os.Create(encryptedFilePath)
|
||||
|
||||
err = encrypt(originFile, encryptedFileWriter, []byte(passphrase))
|
||||
err := encrypt(originFile, encryptedFileWriter, []byte(passphrase))
|
||||
require.NoError(t, err, "Failed to encrypt a file")
|
||||
logs.CloseAndLogErr(encryptedFileWriter)
|
||||
encryptedFileWriter.Close()
|
||||
|
||||
encryptedContent, err := os.ReadFile(encryptedFilePath)
|
||||
require.NoError(t, err, "Couldn't read encrypted file")
|
||||
@@ -66,11 +64,11 @@ func Test_encryptAndDecrypt_withTheSamePassword(t *testing.T) {
|
||||
|
||||
encryptedFileReader, err := os.Open(encryptedFilePath)
|
||||
require.NoError(t, err)
|
||||
defer logs.CloseAndLogErr(encryptedFileReader)
|
||||
defer encryptedFileReader.Close()
|
||||
|
||||
decryptedFileWriter, err := os.Create(decryptedFilePath)
|
||||
require.NoError(t, err)
|
||||
defer logs.CloseAndLogErr(decryptedFileWriter)
|
||||
defer decryptedFileWriter.Close()
|
||||
|
||||
decryptedReader, err := decrypt(encryptedFileReader, []byte(passphrase))
|
||||
if !decryptShouldSucceed {
|
||||
@@ -78,11 +76,9 @@ func Test_encryptAndDecrypt_withTheSamePassword(t *testing.T) {
|
||||
} else {
|
||||
require.NoError(t, err, "Failed to decrypt file indicated by decryptShouldSucceed")
|
||||
|
||||
_, err = io.Copy(decryptedFileWriter, decryptedReader)
|
||||
require.NoError(t, err)
|
||||
io.Copy(decryptedFileWriter, decryptedReader)
|
||||
|
||||
decryptedContent, err := os.ReadFile(decryptedFilePath)
|
||||
require.NoError(t, err)
|
||||
decryptedContent, _ := os.ReadFile(decryptedFilePath)
|
||||
assert.Equal(t, content, decryptedContent, "Original and decrypted content should match")
|
||||
}
|
||||
}
|
||||
@@ -141,53 +137,45 @@ func Test_encryptAndDecrypt_withTheSamePassword(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_encryptAndDecrypt_withStrongPassphrase(t *testing.T) {
|
||||
t.Parallel()
|
||||
const passphrase = "A strong passphrase with special characters: !@#$%^&*()_+"
|
||||
|
||||
testFunc := func(t *testing.T, encrypt encryptFunc, decrypt decryptFunc) {
|
||||
tmpdir := t.TempDir()
|
||||
|
||||
var (
|
||||
originFilePath = filesystem.JoinPaths(tmpdir, "origin2")
|
||||
encryptedFilePath = filesystem.JoinPaths(tmpdir, "encrypted2")
|
||||
decryptedFilePath = filesystem.JoinPaths(tmpdir, "decrypted2")
|
||||
originFilePath = filepath.Join(tmpdir, "origin2")
|
||||
encryptedFilePath = filepath.Join(tmpdir, "encrypted2")
|
||||
decryptedFilePath = filepath.Join(tmpdir, "decrypted2")
|
||||
)
|
||||
|
||||
content := randBytes(500)
|
||||
os.WriteFile(originFilePath, content, 0600)
|
||||
|
||||
err := os.WriteFile(originFilePath, content, 0600)
|
||||
require.NoError(t, err)
|
||||
|
||||
originFile, err := os.Open(originFilePath)
|
||||
require.NoError(t, err)
|
||||
defer logs.CloseAndLogErr(originFile)
|
||||
originFile, _ := os.Open(originFilePath)
|
||||
defer originFile.Close()
|
||||
|
||||
encryptedFileWriter, _ := os.Create(encryptedFilePath)
|
||||
|
||||
err = encrypt(originFile, encryptedFileWriter, []byte(passphrase))
|
||||
err := encrypt(originFile, encryptedFileWriter, []byte(passphrase))
|
||||
require.NoError(t, err, "Failed to encrypt a file")
|
||||
logs.CloseAndLogErr(encryptedFileWriter)
|
||||
encryptedFileWriter.Close()
|
||||
|
||||
encryptedContent, err := os.ReadFile(encryptedFilePath)
|
||||
require.NoError(t, err, "Couldn't read encrypted file")
|
||||
assert.NotEqual(t, encryptedContent, content, "Content wasn't encrypted")
|
||||
|
||||
encryptedFileReader, err := os.Open(encryptedFilePath)
|
||||
require.NoError(t, err)
|
||||
defer logs.CloseAndLogErr(encryptedFileReader)
|
||||
encryptedFileReader, _ := os.Open(encryptedFilePath)
|
||||
defer encryptedFileReader.Close()
|
||||
|
||||
decryptedFileWriter, err := os.Create(decryptedFilePath)
|
||||
require.NoError(t, err)
|
||||
defer logs.CloseAndLogErr(decryptedFileWriter)
|
||||
decryptedFileWriter, _ := os.Create(decryptedFilePath)
|
||||
defer decryptedFileWriter.Close()
|
||||
|
||||
decryptedReader, err := decrypt(encryptedFileReader, []byte(passphrase))
|
||||
require.NoError(t, err, "Failed to decrypt file")
|
||||
|
||||
_, err = io.Copy(decryptedFileWriter, decryptedReader)
|
||||
require.NoError(t, err)
|
||||
io.Copy(decryptedFileWriter, decryptedReader)
|
||||
|
||||
decryptedContent, err := os.ReadFile(decryptedFilePath)
|
||||
require.NoError(t, err)
|
||||
decryptedContent, _ := os.ReadFile(decryptedFilePath)
|
||||
assert.Equal(t, content, decryptedContent, "Original and decrypted content should match")
|
||||
}
|
||||
|
||||
@@ -201,30 +189,26 @@ func Test_encryptAndDecrypt_withStrongPassphrase(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_encryptAndDecrypt_withTheSamePasswordSmallFile(t *testing.T) {
|
||||
t.Parallel()
|
||||
testFunc := func(t *testing.T, encrypt encryptFunc, decrypt decryptFunc) {
|
||||
tmpdir := t.TempDir()
|
||||
|
||||
var (
|
||||
originFilePath = filesystem.JoinPaths(tmpdir, "origin2")
|
||||
encryptedFilePath = filesystem.JoinPaths(tmpdir, "encrypted2")
|
||||
decryptedFilePath = filesystem.JoinPaths(tmpdir, "decrypted2")
|
||||
originFilePath = filepath.Join(tmpdir, "origin2")
|
||||
encryptedFilePath = filepath.Join(tmpdir, "encrypted2")
|
||||
decryptedFilePath = filepath.Join(tmpdir, "decrypted2")
|
||||
)
|
||||
|
||||
content := randBytes(500)
|
||||
err := os.WriteFile(originFilePath, content, 0600)
|
||||
require.NoError(t, err)
|
||||
os.WriteFile(originFilePath, content, 0600)
|
||||
|
||||
originFile, err := os.Open(originFilePath)
|
||||
require.NoError(t, err)
|
||||
defer logs.CloseAndLogErr(originFile)
|
||||
originFile, _ := os.Open(originFilePath)
|
||||
defer originFile.Close()
|
||||
|
||||
encryptedFileWriter, err := os.Create(encryptedFilePath)
|
||||
require.NoError(t, err)
|
||||
encryptedFileWriter, _ := os.Create(encryptedFilePath)
|
||||
|
||||
err = encrypt(originFile, encryptedFileWriter, []byte("passphrase"))
|
||||
err := encrypt(originFile, encryptedFileWriter, []byte("passphrase"))
|
||||
require.NoError(t, err, "Failed to encrypt a file")
|
||||
logs.CloseAndLogErr(encryptedFileWriter)
|
||||
encryptedFileWriter.Close()
|
||||
|
||||
encryptedContent, err := os.ReadFile(encryptedFilePath)
|
||||
require.NoError(t, err, "Couldn't read encrypted file")
|
||||
@@ -232,11 +216,11 @@ func Test_encryptAndDecrypt_withTheSamePasswordSmallFile(t *testing.T) {
|
||||
|
||||
encryptedFileReader, err := os.Open(encryptedFilePath)
|
||||
require.NoError(t, err)
|
||||
defer logs.CloseAndLogErr(encryptedFileReader)
|
||||
defer encryptedFileReader.Close()
|
||||
|
||||
decryptedFileWriter, err := os.Create(decryptedFilePath)
|
||||
require.NoError(t, err)
|
||||
defer logs.CloseAndLogErr(decryptedFileWriter)
|
||||
defer decryptedFileWriter.Close()
|
||||
|
||||
decryptedReader, err := decrypt(encryptedFileReader, []byte("passphrase"))
|
||||
require.NoError(t, err, "Failed to decrypt file")
|
||||
@@ -259,14 +243,13 @@ func Test_encryptAndDecrypt_withTheSamePasswordSmallFile(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_encryptAndDecrypt_withEmptyPassword(t *testing.T) {
|
||||
t.Parallel()
|
||||
testFunc := func(t *testing.T, encrypt encryptFunc, decrypt decryptFunc) {
|
||||
tmpdir := t.TempDir()
|
||||
|
||||
var (
|
||||
originFilePath = filesystem.JoinPaths(tmpdir, "origin")
|
||||
encryptedFilePath = filesystem.JoinPaths(tmpdir, "encrypted")
|
||||
decryptedFilePath = filesystem.JoinPaths(tmpdir, "decrypted")
|
||||
originFilePath = filepath.Join(tmpdir, "origin")
|
||||
encryptedFilePath = filepath.Join(tmpdir, "encrypted")
|
||||
decryptedFilePath = filepath.Join(tmpdir, "decrypted")
|
||||
)
|
||||
|
||||
content := randBytes(1024 * 50)
|
||||
@@ -275,11 +258,11 @@ func Test_encryptAndDecrypt_withEmptyPassword(t *testing.T) {
|
||||
|
||||
originFile, err := os.Open(originFilePath)
|
||||
require.NoError(t, err)
|
||||
defer logs.CloseAndLogErr(originFile)
|
||||
defer originFile.Close()
|
||||
|
||||
encryptedFileWriter, err := os.Create(encryptedFilePath)
|
||||
require.NoError(t, err)
|
||||
defer logs.CloseAndLogErr(encryptedFileWriter)
|
||||
defer encryptedFileWriter.Close()
|
||||
|
||||
err = encrypt(originFile, encryptedFileWriter, []byte(""))
|
||||
require.NoError(t, err, "Failed to encrypt a file")
|
||||
@@ -290,11 +273,11 @@ func Test_encryptAndDecrypt_withEmptyPassword(t *testing.T) {
|
||||
|
||||
encryptedFileReader, err := os.Open(encryptedFilePath)
|
||||
require.NoError(t, err)
|
||||
defer logs.CloseAndLogErr(encryptedFileReader)
|
||||
defer encryptedFileReader.Close()
|
||||
|
||||
decryptedFileWriter, err := os.Create(decryptedFilePath)
|
||||
require.NoError(t, err)
|
||||
defer logs.CloseAndLogErr(decryptedFileWriter)
|
||||
defer decryptedFileWriter.Close()
|
||||
|
||||
decryptedReader, err := decrypt(encryptedFileReader, []byte(""))
|
||||
require.NoError(t, err, "Failed to decrypt file")
|
||||
@@ -317,41 +300,35 @@ func Test_encryptAndDecrypt_withEmptyPassword(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_decryptWithDifferentPassphrase_shouldProduceWrongResult(t *testing.T) {
|
||||
t.Parallel()
|
||||
testFunc := func(t *testing.T, encrypt encryptFunc, decrypt decryptFunc) {
|
||||
tmpdir := t.TempDir()
|
||||
|
||||
var (
|
||||
originFilePath = filesystem.JoinPaths(tmpdir, "origin")
|
||||
encryptedFilePath = filesystem.JoinPaths(tmpdir, "encrypted")
|
||||
decryptedFilePath = filesystem.JoinPaths(tmpdir, "decrypted")
|
||||
originFilePath = filepath.Join(tmpdir, "origin")
|
||||
encryptedFilePath = filepath.Join(tmpdir, "encrypted")
|
||||
decryptedFilePath = filepath.Join(tmpdir, "decrypted")
|
||||
)
|
||||
|
||||
content := randBytes(1034)
|
||||
err := os.WriteFile(originFilePath, content, 0600)
|
||||
require.NoError(t, err)
|
||||
os.WriteFile(originFilePath, content, 0600)
|
||||
|
||||
originFile, err := os.Open(originFilePath)
|
||||
require.NoError(t, err)
|
||||
defer logs.CloseAndLogErr(originFile)
|
||||
originFile, _ := os.Open(originFilePath)
|
||||
defer originFile.Close()
|
||||
|
||||
encryptedFileWriter, err := os.Create(encryptedFilePath)
|
||||
require.NoError(t, err)
|
||||
defer logs.CloseAndLogErr(encryptedFileWriter)
|
||||
encryptedFileWriter, _ := os.Create(encryptedFilePath)
|
||||
defer encryptedFileWriter.Close()
|
||||
|
||||
err = encrypt(originFile, encryptedFileWriter, []byte("passphrase"))
|
||||
err := encrypt(originFile, encryptedFileWriter, []byte("passphrase"))
|
||||
require.NoError(t, err, "Failed to encrypt a file")
|
||||
encryptedContent, err := os.ReadFile(encryptedFilePath)
|
||||
require.NoError(t, err, "Couldn't read encrypted file")
|
||||
assert.NotEqual(t, encryptedContent, content, "Content wasn't encrypted")
|
||||
|
||||
encryptedFileReader, err := os.Open(encryptedFilePath)
|
||||
require.NoError(t, err)
|
||||
defer logs.CloseAndLogErr(encryptedFileReader)
|
||||
encryptedFileReader, _ := os.Open(encryptedFilePath)
|
||||
defer encryptedFileReader.Close()
|
||||
|
||||
decryptedFileWriter, err := os.Create(decryptedFilePath)
|
||||
require.NoError(t, err)
|
||||
defer logs.CloseAndLogErr(decryptedFileWriter)
|
||||
decryptedFileWriter, _ := os.Create(decryptedFilePath)
|
||||
defer decryptedFileWriter.Close()
|
||||
|
||||
_, err = decrypt(encryptedFileReader, []byte("garbage"))
|
||||
require.Error(t, err, "Should not allow decrypt with wrong passphrase")
|
||||
@@ -389,7 +366,6 @@ func legacyAesEncrypt(input io.Reader, output io.Writer, passphrase []byte) erro
|
||||
}
|
||||
|
||||
func Test_hasEncryptedHeader(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
name string
|
||||
data []byte
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
)
|
||||
|
||||
func TestCreateSignature(t *testing.T) {
|
||||
t.Parallel()
|
||||
var s = NewECDSAService("secret")
|
||||
|
||||
privKey, pubKey, err := s.GenerateKeyPair()
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
)
|
||||
|
||||
func TestService_Hash(t *testing.T) {
|
||||
t.Parallel()
|
||||
var s = Service{}
|
||||
|
||||
type args struct {
|
||||
@@ -56,7 +55,6 @@ func TestService_Hash(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestHash(t *testing.T) {
|
||||
t.Parallel()
|
||||
s := Service{}
|
||||
|
||||
hash, err := s.Hash("Passw0rd!")
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"io"
|
||||
"slices"
|
||||
)
|
||||
|
||||
type Nonce struct {
|
||||
@@ -46,7 +45,7 @@ func (n *Nonce) Value() []byte {
|
||||
|
||||
func (n *Nonce) Increment() error {
|
||||
// Start incrementing from the least significant byte
|
||||
for i := range slices.Backward(n.val) {
|
||||
for i := len(n.val) - 1; i >= 0; i-- {
|
||||
// Increment the current byte
|
||||
n.val[i]++
|
||||
|
||||
|
||||
@@ -92,9 +92,7 @@ func CreateTLSConfigurationFromDisk(config portainer.TLSConfiguration) (*tls.Con
|
||||
}
|
||||
|
||||
func createTLSConfigurationFromDisk(fipsEnabled bool, config portainer.TLSConfiguration) (*tls.Config, error) { //nolint:forbidigo
|
||||
if !config.TLS && fipsEnabled {
|
||||
return nil, fips.ErrTLSRequired
|
||||
} else if !config.TLS {
|
||||
if !config.TLS {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
)
|
||||
|
||||
func TestCreateTLSConfiguration(t *testing.T) {
|
||||
t.Parallel()
|
||||
// InsecureSkipVerify = false
|
||||
config := CreateTLSConfiguration(false)
|
||||
require.Equal(t, config.MinVersion, uint16(tls.VersionTLS12)) //nolint:forbidigo
|
||||
@@ -23,7 +22,6 @@ func TestCreateTLSConfiguration(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCreateTLSConfigurationFIPS(t *testing.T) {
|
||||
t.Parallel()
|
||||
fips := true
|
||||
|
||||
fipsCipherSuites := []uint16{
|
||||
@@ -44,7 +42,6 @@ func TestCreateTLSConfigurationFIPS(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCreateTLSConfigurationFromBytes(t *testing.T) {
|
||||
t.Parallel()
|
||||
// No TLS
|
||||
config, err := CreateTLSConfigurationFromBytes(false, nil, nil, nil, false, false)
|
||||
require.NoError(t, err)
|
||||
@@ -62,7 +59,6 @@ func TestCreateTLSConfigurationFromBytes(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCreateTLSConfigurationFromDisk(t *testing.T) {
|
||||
t.Parallel()
|
||||
// No TLS
|
||||
config, err := CreateTLSConfigurationFromDisk(portainer.TLSConfiguration{})
|
||||
require.NoError(t, err)
|
||||
@@ -78,7 +74,6 @@ func TestCreateTLSConfigurationFromDisk(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCreateTLSConfigurationFromDiskFIPS(t *testing.T) {
|
||||
t.Parallel()
|
||||
fips := true
|
||||
|
||||
// Skipping TLS verifications cannot be done in FIPS mode
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package boltdb
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -42,8 +40,6 @@ type DbConnection struct {
|
||||
isEncrypted bool
|
||||
Compact bool
|
||||
|
||||
gcm cipher.AEAD
|
||||
|
||||
*bolt.DB
|
||||
}
|
||||
|
||||
@@ -79,28 +75,8 @@ func (connection *DbConnection) GetDatabaseFileSize() (int64, error) {
|
||||
return file.Size(), nil
|
||||
}
|
||||
|
||||
func (connection *DbConnection) SetEncrypted(flag bool) error {
|
||||
func (connection *DbConnection) SetEncrypted(flag bool) {
|
||||
connection.isEncrypted = flag
|
||||
|
||||
if !flag || connection.EncryptionKey == nil {
|
||||
connection.gcm = nil
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
block, err := aes.NewCipher(connection.EncryptionKey)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating AES cipher for database encryption: %w", err)
|
||||
}
|
||||
|
||||
gcm, err := cipher.NewGCMWithRandomNonce(block)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating GCM cipher for database encryption: %w", err)
|
||||
}
|
||||
|
||||
connection.gcm = gcm
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Return true if the database is encrypted
|
||||
@@ -124,9 +100,7 @@ func (connection *DbConnection) NeedsEncryptionMigration() (bool, error) {
|
||||
|
||||
// If we have a loaded encryption key, always set encrypted
|
||||
if connection.EncryptionKey != nil {
|
||||
if err := connection.SetEncrypted(true); err != nil {
|
||||
return false, err
|
||||
}
|
||||
connection.SetEncrypted(true)
|
||||
}
|
||||
|
||||
// Check for portainer.db
|
||||
|
||||
@@ -2,6 +2,7 @@ package boltdb
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
|
||||
"github.com/portainer/portainer/api/filesystem"
|
||||
@@ -12,7 +13,6 @@ import (
|
||||
)
|
||||
|
||||
func Test_NeedsEncryptionMigration(t *testing.T) {
|
||||
t.Parallel()
|
||||
// Test the specific scenarios mentioned in NeedsEncryptionMigration
|
||||
|
||||
// i.e.
|
||||
@@ -96,42 +96,24 @@ func Test_NeedsEncryptionMigration(t *testing.T) {
|
||||
|
||||
if tc.dbname == "both" {
|
||||
// Special case. If portainer.db and portainer.edb exist.
|
||||
dbFile1 := filesystem.JoinPaths(connection.Path, DatabaseFileName)
|
||||
dbFile1 := path.Join(connection.Path, DatabaseFileName)
|
||||
f, _ := os.Create(dbFile1)
|
||||
f.Close()
|
||||
defer os.Remove(dbFile1)
|
||||
|
||||
err := f.Close()
|
||||
require.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
err := os.Remove(dbFile1)
|
||||
require.NoError(t, err)
|
||||
}()
|
||||
|
||||
dbFile2 := filesystem.JoinPaths(connection.Path, EncryptedDatabaseFileName)
|
||||
dbFile2 := path.Join(connection.Path, EncryptedDatabaseFileName)
|
||||
f, _ = os.Create(dbFile2)
|
||||
|
||||
err = f.Close()
|
||||
require.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
err := os.Remove(dbFile2)
|
||||
require.NoError(t, err)
|
||||
}()
|
||||
f.Close()
|
||||
defer os.Remove(dbFile2)
|
||||
} else if tc.dbname != "" {
|
||||
dbFile := filesystem.JoinPaths(connection.Path, tc.dbname)
|
||||
dbFile := path.Join(connection.Path, tc.dbname)
|
||||
f, _ := os.Create(dbFile)
|
||||
|
||||
err := f.Close()
|
||||
require.NoError(t, err)
|
||||
|
||||
defer func() {
|
||||
err := os.Remove(dbFile)
|
||||
require.NoError(t, err)
|
||||
}()
|
||||
f.Close()
|
||||
defer os.Remove(dbFile)
|
||||
}
|
||||
|
||||
if tc.key {
|
||||
connection.EncryptionKey = secretToEncryptionKey("secret")
|
||||
connection.EncryptionKey = []byte("secret")
|
||||
}
|
||||
|
||||
result, err := connection.NeedsEncryptionMigration()
|
||||
@@ -142,59 +124,7 @@ func Test_NeedsEncryptionMigration(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetEncrypted_InvalidKeyReturnsError(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
conn := DbConnection{EncryptionKey: []byte("bad")}
|
||||
err := conn.SetEncrypted(true)
|
||||
require.Error(t, err)
|
||||
require.Nil(t, conn.gcm)
|
||||
}
|
||||
|
||||
func TestSetEncrypted_NilKeyDoesNotSetGCM(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
conn := DbConnection{}
|
||||
err := conn.SetEncrypted(true)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, conn.gcm)
|
||||
}
|
||||
|
||||
func TestSetEncrypted_EnableThenDisableStopsEncryption(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
key := secretToEncryptionKey(passphrase)
|
||||
conn := DbConnection{EncryptionKey: key}
|
||||
|
||||
err := conn.SetEncrypted(true)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, conn.gcm)
|
||||
|
||||
err = conn.SetEncrypted(false)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, conn.gcm)
|
||||
|
||||
// MarshalObject must return plaintext after encryption is disabled
|
||||
data, err := conn.MarshalObject("hello")
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, "hello", string(data))
|
||||
}
|
||||
|
||||
func TestNeedsEncryptionMigration_InvalidKeyError(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
conn := DbConnection{
|
||||
Path: t.TempDir(),
|
||||
EncryptionKey: []byte("bad"),
|
||||
}
|
||||
|
||||
result, err := conn.NeedsEncryptionMigration()
|
||||
require.Error(t, err)
|
||||
require.False(t, result)
|
||||
}
|
||||
|
||||
func TestDBCompaction(t *testing.T) {
|
||||
t.Parallel()
|
||||
db := &DbConnection{Path: t.TempDir()}
|
||||
|
||||
err := db.Open()
|
||||
@@ -206,8 +136,7 @@ func TestDBCompaction(t *testing.T) {
|
||||
return err
|
||||
}
|
||||
|
||||
err = b.Put([]byte("key"), []byte("value"))
|
||||
require.NoError(t, err)
|
||||
b.Put([]byte("key"), []byte("value"))
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -3,7 +3,6 @@ package boltdb
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/portainer/portainer/api/logs"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/segmentio/encoding/json"
|
||||
bolt "go.etcd.io/bbolt"
|
||||
@@ -38,7 +37,7 @@ func (c *DbConnection) ExportJSON(databasePath string, metadata bool) ([]byte, e
|
||||
if err != nil {
|
||||
return []byte("{}"), err
|
||||
}
|
||||
defer logs.CloseAndLogErr(connection)
|
||||
defer connection.Close()
|
||||
|
||||
backup := make(map[string]any)
|
||||
if metadata {
|
||||
|
||||
@@ -2,6 +2,7 @@ package boltdb
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
@@ -27,29 +28,29 @@ func (connection *DbConnection) MarshalObject(object any) ([]byte, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if connection.gcm == nil {
|
||||
if connection.getEncryptionKey() == nil {
|
||||
return buf.Bytes(), nil
|
||||
}
|
||||
|
||||
return encrypt(buf.Bytes(), connection.gcm), nil
|
||||
return encrypt(buf.Bytes(), connection.getEncryptionKey())
|
||||
}
|
||||
|
||||
// UnmarshalObject decodes an object from binary data
|
||||
func (connection *DbConnection) UnmarshalObject(data []byte, object any) error {
|
||||
var err error
|
||||
if connection.gcm != nil {
|
||||
data, err = decrypt(data, connection.gcm)
|
||||
if connection.getEncryptionKey() != nil {
|
||||
data, err = decrypt(data, connection.getEncryptionKey())
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "Failed decrypting object")
|
||||
}
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(data, object); err != nil {
|
||||
if e := json.Unmarshal(data, object); e != nil {
|
||||
// Special case for the VERSION bucket. Here we're not using json
|
||||
// So we need to return it as a string
|
||||
s, ok := object.(*string)
|
||||
if !ok {
|
||||
return errors.Wrap(err, "Failed unmarshalling object")
|
||||
return errors.Wrap(err, e.Error())
|
||||
}
|
||||
|
||||
*s = string(data)
|
||||
@@ -58,23 +59,48 @@ func (connection *DbConnection) UnmarshalObject(data []byte, object any) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func encrypt(plaintext []byte, gcm cipher.AEAD) []byte {
|
||||
return gcm.Seal(nil, nil, plaintext, nil)
|
||||
// mmm, don't have a KMS .... aes GCM seems the most likely from
|
||||
// https://gist.github.com/atoponce/07d8d4c833873be2f68c34f9afc5a78a#symmetric-encryption
|
||||
|
||||
func encrypt(plaintext []byte, passphrase []byte) (encrypted []byte, err error) {
|
||||
block, err := aes.NewCipher(passphrase)
|
||||
if err != nil {
|
||||
return encrypted, err
|
||||
}
|
||||
|
||||
// NewGCMWithRandomNonce in go 1.24 handles setting up the nonce and adding it to the encrypted output
|
||||
gcm, err := cipher.NewGCMWithRandomNonce(block)
|
||||
if err != nil {
|
||||
return encrypted, err
|
||||
}
|
||||
|
||||
return gcm.Seal(nil, nil, plaintext, nil), nil
|
||||
}
|
||||
|
||||
func decrypt(encrypted []byte, gcm cipher.AEAD) ([]byte, error) {
|
||||
func decrypt(encrypted []byte, passphrase []byte) (plaintextByte []byte, err error) {
|
||||
if string(encrypted) == "false" {
|
||||
return []byte("false"), nil
|
||||
}
|
||||
|
||||
if len(encrypted) < gcm.Overhead() {
|
||||
block, err := aes.NewCipher(passphrase)
|
||||
if err != nil {
|
||||
return encrypted, errors.Wrap(err, "Error creating cypher block")
|
||||
}
|
||||
|
||||
// NewGCMWithRandomNonce in go 1.24 handles reading the nonce from the encrypted input for us
|
||||
gcm, err := cipher.NewGCMWithRandomNonce(block)
|
||||
if err != nil {
|
||||
return encrypted, errors.Wrap(err, "Error creating GCM")
|
||||
}
|
||||
|
||||
if len(encrypted) < gcm.NonceSize() {
|
||||
return encrypted, errEncryptedStringTooShort
|
||||
}
|
||||
|
||||
plaintextByte, err := gcm.Open(nil, nil, encrypted, nil)
|
||||
plaintextByte, err = gcm.Open(nil, nil, encrypted, nil)
|
||||
if err != nil {
|
||||
return encrypted, errors.Wrap(err, "Error decrypting text")
|
||||
}
|
||||
|
||||
return plaintextByte, nil
|
||||
return plaintextByte, err
|
||||
}
|
||||
|
||||
@@ -10,14 +10,14 @@ import (
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const (
|
||||
jsonobject = `{"LogoURL":"","BlackListedLabels":[],"AuthenticationMethod":1,"InternalAuthSettings": {"RequiredPasswordLength": 12}"LDAPSettings":{"AnonymousMode":true,"ReaderDN":"","URL":"","TLSConfig":{"TLS":false,"TLSSkipVerify":false},"StartTLS":false,"SearchSettings":[{"BaseDN":"","Filter":"","UserNameAttribute":""}],"GroupSearchSettings":[{"GroupBaseDN":"","GroupFilter":"","GroupAttribute":""}],"AutoCreateUsers":true},"OAuthSettings":{"ClientID":"","AccessTokenURI":"","AuthorizationURI":"","ResourceURI":"","RedirectURI":"","UserIdentifier":"","Scopes":"","OAuthAutoCreateUsers":false,"DefaultTeamID":0,"SSO":true,"LogoutURI":"","KubeSecretKey":"j0zLVtY/lAWBk62ByyF0uP80SOXaitsABP0TTJX8MhI="},"FeatureFlagSettings":{},"SnapshotInterval":"5m","TemplatesURL":"https://raw.githubusercontent.com/portainer/templates/master/templates-2.0.json","EdgeAgentCheckinInterval":5,"EnableEdgeComputeFeatures":false,"UserSessionTimeout":"8h","KubeconfigExpiry":"0","HelmRepositoryURL":"https://charts.bitnami.com/bitnami","KubectlShellImage":"portainer/kubectl-shell","DisplayDonationHeader":false,"DisplayExternalContributors":false,"EnableHostManagementFeatures":false,"AllowVolumeBrowserForRegularUsers":false,"AllowBindMountsForRegularUsers":false,"AllowPrivilegedModeForRegularUsers":false,"AllowHostNamespaceForRegularUsers":false,"AllowStackManagementForRegularUsers":false,"AllowDeviceMappingForRegularUsers":false,"AllowContainerCapabilitiesForRegularUsers":false}`
|
||||
jsonobject = `{"LogoURL":"","BlackListedLabels":[],"AuthenticationMethod":1,"InternalAuthSettings": {"RequiredPasswordLength": 12}"LDAPSettings":{"AnonymousMode":true,"ReaderDN":"","URL":"","TLSConfig":{"TLS":false,"TLSSkipVerify":false},"StartTLS":false,"SearchSettings":[{"BaseDN":"","Filter":"","UserNameAttribute":""}],"GroupSearchSettings":[{"GroupBaseDN":"","GroupFilter":"","GroupAttribute":""}],"AutoCreateUsers":true},"OAuthSettings":{"ClientID":"","AccessTokenURI":"","AuthorizationURI":"","ResourceURI":"","RedirectURI":"","UserIdentifier":"","Scopes":"","OAuthAutoCreateUsers":false,"DefaultTeamID":0,"SSO":true,"LogoutURI":"","KubeSecretKey":"j0zLVtY/lAWBk62ByyF0uP80SOXaitsABP0TTJX8MhI="},"OpenAMTConfiguration":{"Enabled":false,"MPSServer":"","MPSUser":"","MPSPassword":"","MPSToken":"","CertFileContent":"","CertFileName":"","CertFilePassword":"","DomainName":""},"FeatureFlagSettings":{},"SnapshotInterval":"5m","TemplatesURL":"https://raw.githubusercontent.com/portainer/templates/master/templates-2.0.json","EdgeAgentCheckinInterval":5,"EnableEdgeComputeFeatures":false,"UserSessionTimeout":"8h","KubeconfigExpiry":"0","EnableTelemetry":true,"HelmRepositoryURL":"https://charts.bitnami.com/bitnami","KubectlShellImage":"portainer/kubectl-shell","DisplayDonationHeader":false,"DisplayExternalContributors":false,"EnableHostManagementFeatures":false,"AllowVolumeBrowserForRegularUsers":false,"AllowBindMountsForRegularUsers":false,"AllowPrivilegedModeForRegularUsers":false,"AllowHostNamespaceForRegularUsers":false,"AllowStackManagementForRegularUsers":false,"AllowDeviceMappingForRegularUsers":false,"AllowContainerCapabilitiesForRegularUsers":false}`
|
||||
passphrase = "my secret key"
|
||||
)
|
||||
|
||||
@@ -27,10 +27,9 @@ func secretToEncryptionKey(passphrase string) []byte {
|
||||
}
|
||||
|
||||
func Test_MarshalObjectUnencrypted(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
uuid := uuid.New()
|
||||
uuid := uuid.Must(uuid.NewV4())
|
||||
|
||||
tests := []struct {
|
||||
object any
|
||||
@@ -102,7 +101,6 @@ func Test_MarshalObjectUnencrypted(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_UnMarshalObjectUnencrypted(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
// Based on actual data entering and what we expect out of the function
|
||||
@@ -144,7 +142,6 @@ func Test_UnMarshalObjectUnencrypted(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_ObjectMarshallingEncrypted(t *testing.T) {
|
||||
t.Parallel()
|
||||
is := assert.New(t)
|
||||
|
||||
// Based on actual data entering and what we expect out of the function
|
||||
@@ -170,10 +167,7 @@ func Test_ObjectMarshallingEncrypted(t *testing.T) {
|
||||
}
|
||||
|
||||
key := secretToEncryptionKey(passphrase)
|
||||
conn := DbConnection{EncryptionKey: key}
|
||||
err := conn.SetEncrypted(true)
|
||||
require.NoError(t, err)
|
||||
|
||||
conn := DbConnection{EncryptionKey: key, isEncrypted: true}
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("%s -> %s", test.object, test.expected), func(t *testing.T) {
|
||||
|
||||
@@ -190,7 +184,6 @@ func Test_ObjectMarshallingEncrypted(t *testing.T) {
|
||||
}
|
||||
|
||||
func Test_NonceSources(t *testing.T) {
|
||||
t.Parallel()
|
||||
// ensure that the new go 1.24 NewGCMWithRandomNonce works correctly with
|
||||
// the old way of creating and including the nonce
|
||||
|
||||
@@ -235,16 +228,13 @@ func Test_NonceSources(t *testing.T) {
|
||||
return plaintext, err
|
||||
}
|
||||
|
||||
encryptNewFn := encrypt
|
||||
decryptNewFn := decrypt
|
||||
|
||||
passphrase := make([]byte, 32)
|
||||
_, err := io.ReadFull(rand.Reader, passphrase)
|
||||
require.NoError(t, err)
|
||||
|
||||
block, err := aes.NewCipher(passphrase)
|
||||
require.NoError(t, err)
|
||||
|
||||
gcm, err := cipher.NewGCMWithRandomNonce(block)
|
||||
require.NoError(t, err)
|
||||
|
||||
junk := make([]byte, 1024)
|
||||
_, err = io.ReadFull(rand.Reader, junk)
|
||||
require.NoError(t, err)
|
||||
@@ -269,12 +259,13 @@ func Test_NonceSources(t *testing.T) {
|
||||
enc, err = encryptOldFn(plain, passphrase)
|
||||
require.NoError(t, err)
|
||||
|
||||
dec, err = decrypt(enc, gcm)
|
||||
dec, err = decryptNewFn(enc, passphrase)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, plain, dec)
|
||||
|
||||
enc = encrypt(plain, gcm)
|
||||
enc, err = encryptNewFn(plain, passphrase)
|
||||
require.NoError(t, err)
|
||||
|
||||
dec, err = decryptOldFn(enc, passphrase)
|
||||
require.NoError(t, err)
|
||||
@@ -282,110 +273,3 @@ func Test_NonceSources(t *testing.T) {
|
||||
require.Equal(t, plain, dec)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecrypt_FalseStringBypassesDecryption(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
key := secretToEncryptionKey(passphrase)
|
||||
block, err := aes.NewCipher(key)
|
||||
require.NoError(t, err)
|
||||
|
||||
gcm, err := cipher.NewGCMWithRandomNonce(block)
|
||||
require.NoError(t, err)
|
||||
|
||||
result, err := decrypt([]byte("false"), gcm)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []byte("false"), result)
|
||||
}
|
||||
|
||||
func TestDecrypt_ShortDataReturnsError(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
key := secretToEncryptionKey(passphrase)
|
||||
block, err := aes.NewCipher(key)
|
||||
require.NoError(t, err)
|
||||
|
||||
gcm, err := cipher.NewGCMWithRandomNonce(block)
|
||||
require.NoError(t, err)
|
||||
|
||||
short := []byte("short")
|
||||
result, err := decrypt(short, gcm)
|
||||
require.ErrorIs(t, err, errEncryptedStringTooShort)
|
||||
require.Equal(t, short, result)
|
||||
}
|
||||
|
||||
func TestDecrypt_CorruptDataReturnsError(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
key := secretToEncryptionKey(passphrase)
|
||||
block, err := aes.NewCipher(key)
|
||||
require.NoError(t, err)
|
||||
|
||||
gcm, err := cipher.NewGCMWithRandomNonce(block)
|
||||
require.NoError(t, err)
|
||||
|
||||
// 30 bytes passes the length check but fails authentication
|
||||
corrupted := make([]byte, 30)
|
||||
_, err = io.ReadFull(rand.Reader, corrupted)
|
||||
require.NoError(t, err)
|
||||
|
||||
result, err := decrypt(corrupted, gcm)
|
||||
require.Error(t, err)
|
||||
require.Equal(t, corrupted, result)
|
||||
}
|
||||
|
||||
// BenchmarkEncryptCachedCipher measures the new approach: cipher created once and reused.
|
||||
func BenchmarkEncryptCachedCipher(b *testing.B) {
|
||||
key := secretToEncryptionKey(passphrase)
|
||||
conn := DbConnection{EncryptionKey: key}
|
||||
err := conn.SetEncrypted(true)
|
||||
require.NoError(b, err)
|
||||
|
||||
data := []byte(jsonobject)
|
||||
|
||||
b.ResetTimer()
|
||||
|
||||
for b.Loop() {
|
||||
_ = encrypt(data, conn.gcm)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkEncryptPerCallCipher measures the old approach: cipher created on every call.
|
||||
func BenchmarkEncryptPerCallCipher(b *testing.B) {
|
||||
key := secretToEncryptionKey(passphrase)
|
||||
data := []byte(jsonobject)
|
||||
|
||||
b.ResetTimer()
|
||||
|
||||
for b.Loop() {
|
||||
block, err := aes.NewCipher(key)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
gcm, err := cipher.NewGCMWithRandomNonce(block)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
|
||||
_ = gcm.Seal(nil, nil, data, nil)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkEncryptCachedCipherParallel verifies the cached cipher is safe for concurrent use.
|
||||
func BenchmarkEncryptCachedCipherParallel(b *testing.B) {
|
||||
key := secretToEncryptionKey(passphrase)
|
||||
conn := DbConnection{EncryptionKey: key}
|
||||
err := conn.SetEncrypted(true)
|
||||
require.NoError(b, err)
|
||||
|
||||
data := []byte(jsonobject)
|
||||
|
||||
b.ResetTimer()
|
||||
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
_ = encrypt(data, conn.gcm)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -40,10 +40,10 @@ func (tx *DbTransaction) GetRawBytes(bucketName string, key []byte) ([]byte, err
|
||||
return nil, fmt.Errorf("%w (bucket=%s, key=%s)", dserrors.ErrObjectNotFound, bucketName, keyToString(key))
|
||||
}
|
||||
|
||||
if tx.conn.gcm != nil {
|
||||
if tx.conn.getEncryptionKey() != nil {
|
||||
var err error
|
||||
|
||||
if value, err = decrypt(value, tx.conn.gcm); err != nil {
|
||||
if value, err = decrypt(value, tx.conn.getEncryptionKey()); err != nil {
|
||||
return value, errors.Wrap(err, "Failed decrypting object")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,12 +2,10 @@ package boltdb
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/dataservices"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const testBucketName = "test-bucket"
|
||||
@@ -19,56 +17,70 @@ type testStruct struct {
|
||||
}
|
||||
|
||||
func TestTxs(t *testing.T) {
|
||||
t.Parallel()
|
||||
conn := DbConnection{Path: t.TempDir()}
|
||||
conn := DbConnection{
|
||||
Path: t.TempDir(),
|
||||
}
|
||||
|
||||
err := conn.Open()
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
err := conn.Close()
|
||||
require.NoError(t, err)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
// Error propagation
|
||||
err = conn.UpdateTx(func(tx portainer.Transaction) error {
|
||||
return errors.New("this is an error")
|
||||
})
|
||||
require.Error(t, err)
|
||||
if err == nil {
|
||||
t.Fatal("an error was expected, got nil instead")
|
||||
}
|
||||
|
||||
// Create an object
|
||||
newObj := testStruct{Key: "key", Value: "value"}
|
||||
newObj := testStruct{
|
||||
Key: "key",
|
||||
Value: "value",
|
||||
}
|
||||
|
||||
err = conn.UpdateTx(func(tx portainer.Transaction) error {
|
||||
if err := tx.SetServiceName(testBucketName); err != nil {
|
||||
err = tx.SetServiceName(testBucketName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.CreateObjectWithId(testBucketName, testId, newObj)
|
||||
})
|
||||
require.NoError(t, err)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
obj := testStruct{}
|
||||
err = conn.ViewTx(func(tx portainer.Transaction) error {
|
||||
return tx.GetObject(testBucketName, conn.ConvertToKey(testId), &obj)
|
||||
})
|
||||
require.NoError(t, err)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if obj.Key != newObj.Key || obj.Value != newObj.Value {
|
||||
t.Fatalf("expected %s:%s, got %s:%s instead", newObj.Key, newObj.Value, obj.Key, obj.Value)
|
||||
}
|
||||
|
||||
// Update an object
|
||||
updatedObj := testStruct{Key: "updated-key", Value: "updated-value"}
|
||||
updatedObj := testStruct{
|
||||
Key: "updated-key",
|
||||
Value: "updated-value",
|
||||
}
|
||||
|
||||
err = conn.UpdateTx(func(tx portainer.Transaction) error {
|
||||
return tx.UpdateObject(testBucketName, conn.ConvertToKey(testId), &updatedObj)
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = conn.ViewTx(func(tx portainer.Transaction) error {
|
||||
return tx.GetObject(testBucketName, conn.ConvertToKey(testId), &obj)
|
||||
})
|
||||
require.NoError(t, err)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if obj.Key != updatedObj.Key || obj.Value != updatedObj.Value {
|
||||
t.Fatalf("expected %s:%s, got %s:%s instead", updatedObj.Key, updatedObj.Value, obj.Key, obj.Value)
|
||||
@@ -78,12 +90,16 @@ func TestTxs(t *testing.T) {
|
||||
err = conn.UpdateTx(func(tx portainer.Transaction) error {
|
||||
return tx.DeleteObject(testBucketName, conn.ConvertToKey(testId))
|
||||
})
|
||||
require.NoError(t, err)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = conn.ViewTx(func(tx portainer.Transaction) error {
|
||||
return tx.GetObject(testBucketName, conn.ConvertToKey(testId), &obj)
|
||||
})
|
||||
require.True(t, dataservices.IsErrObjectNotFound(err))
|
||||
if !dataservices.IsErrObjectNotFound(err) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Get next identifier
|
||||
err = conn.UpdateTx(func(tx portainer.Transaction) error {
|
||||
@@ -96,65 +112,15 @@ func TestTxs(t *testing.T) {
|
||||
|
||||
return nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Try to write in a read transaction
|
||||
err = conn.ViewTx(func(tx portainer.Transaction) error {
|
||||
return tx.CreateObjectWithId(testBucketName, testId, newObj)
|
||||
})
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func BenchmarkGetAll(b *testing.B) {
|
||||
const endpointBucket = "endpoints"
|
||||
const n = 10000
|
||||
|
||||
conn := DbConnection{Path: b.TempDir()}
|
||||
|
||||
err := conn.Open()
|
||||
require.NoError(b, err)
|
||||
b.Cleanup(func() {
|
||||
err := conn.Close()
|
||||
require.NoError(b, err)
|
||||
})
|
||||
|
||||
err = conn.UpdateTx(func(tx portainer.Transaction) error {
|
||||
if err := tx.SetServiceName(endpointBucket); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := 1; i <= n; i++ {
|
||||
ep := portainer.Endpoint{
|
||||
ID: portainer.EndpointID(i),
|
||||
Name: "env-" + strconv.Itoa(i),
|
||||
Type: portainer.DockerEnvironment,
|
||||
URL: "tcp://192.168.1." + strconv.Itoa(i%254+1) + ":2375",
|
||||
PublicURL: "https://env-" + strconv.Itoa(i) + ".example.com",
|
||||
GroupID: portainer.EndpointGroupID(i%10 + 1),
|
||||
TagIDs: []portainer.TagID{portainer.TagID(i%5 + 1), portainer.TagID(i%3 + 1)},
|
||||
LastCheckInDate: int64(i) * 1000,
|
||||
EdgeID: "edge-" + strconv.Itoa(i),
|
||||
}
|
||||
|
||||
if err := tx.CreateObjectWithId(endpointBucket, i, &ep); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
require.NoError(b, err)
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for b.Loop() {
|
||||
var collection []portainer.Endpoint
|
||||
|
||||
if err := conn.ViewTx(func(tx portainer.Transaction) error {
|
||||
return tx.GetAll(endpointBucket, new(portainer.Endpoint), dataservices.AppendFn(&collection))
|
||||
}); err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
if err == nil {
|
||||
t.Fatal("an error was expected, got nil instead")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
)
|
||||
|
||||
func TestNewDatabase(t *testing.T) {
|
||||
t.Parallel()
|
||||
dbPath := filesystem.JoinPaths(t.TempDir(), "test.db")
|
||||
connection, err := NewDatabase("boltdb", dbPath, nil, false)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -1,131 +0,0 @@
|
||||
package allowlist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
lru "github.com/hashicorp/golang-lru"
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/dataservices"
|
||||
"github.com/portainer/portainer/pkg/libhttp/ssrf"
|
||||
)
|
||||
|
||||
const (
|
||||
BucketName = "allowlist"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
baseService dataservices.BaseDataService[portainer.AllowList, portainer.AllowListKey]
|
||||
cache *lru.Cache
|
||||
}
|
||||
|
||||
func (service *Service) BucketName() string {
|
||||
return service.baseService.BucketName()
|
||||
}
|
||||
|
||||
func NewService(connection portainer.Connection) (*Service, error) {
|
||||
err := connection.SetServiceName(BucketName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
service := &Service{
|
||||
baseService: dataservices.BaseDataService[portainer.AllowList, portainer.AllowListKey]{
|
||||
Bucket: BucketName,
|
||||
Connection: connection,
|
||||
},
|
||||
}
|
||||
|
||||
err = service.populateCache()
|
||||
|
||||
return service, err
|
||||
}
|
||||
|
||||
func (service *Service) populateCache() error {
|
||||
allowListKeys := []portainer.AllowListKey{portainer.AllowListSSRF}
|
||||
cache, err := lru.New(len(allowListKeys))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, k := range allowListKeys {
|
||||
allowList, err := service.baseService.Read(k)
|
||||
if dataservices.IsErrObjectNotFound(err) {
|
||||
allowList = &portainer.AllowList{
|
||||
ID: k,
|
||||
Mode: portainer.SSRFModeOff,
|
||||
Entries: []string{},
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
parsedAllowList := ssrf.ParseAllowedHosts(allowList.Entries)
|
||||
parsedAllowList.Mode = allowList.Mode
|
||||
|
||||
cache.Add(k, &parsedAllowList)
|
||||
}
|
||||
|
||||
service.cache = cache
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (service *Service) Tx(tx portainer.Transaction) *ServiceTx {
|
||||
return &ServiceTx{
|
||||
baseService: service.baseService.Tx(tx),
|
||||
cache: service.cache,
|
||||
}
|
||||
}
|
||||
|
||||
func (service *Service) Read(id portainer.AllowListKey) (*portainer.AllowList, error) {
|
||||
var result *portainer.AllowList
|
||||
if err := service.baseService.Connection.ViewTx(func(tx portainer.Transaction) error {
|
||||
var err error
|
||||
result, err = service.Tx(tx).Read(id)
|
||||
return err
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (service *Service) ReadAll() ([]portainer.AllowList, error) {
|
||||
var result []portainer.AllowList
|
||||
if err := service.baseService.Connection.ViewTx(func(tx portainer.Transaction) error {
|
||||
var err error
|
||||
result, err = service.Tx(tx).ReadAll()
|
||||
return err
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (service *Service) ReadParsed(id portainer.AllowListKey) (*portainer.ParsedAllowList, error) {
|
||||
allowListAny, ok := service.cache.Get(id)
|
||||
if ok {
|
||||
allowList, ok := allowListAny.(*portainer.ParsedAllowList)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("expected ParsedAllowList in cache but got %T", allowListAny)
|
||||
}
|
||||
|
||||
return allowList, nil
|
||||
}
|
||||
|
||||
var result *portainer.ParsedAllowList
|
||||
err := service.baseService.Connection.ViewTx(func(tx portainer.Transaction) error {
|
||||
var err error
|
||||
result, err = service.Tx(tx).ReadParsed(id)
|
||||
return err
|
||||
})
|
||||
|
||||
return result, err
|
||||
}
|
||||
|
||||
func (service *Service) Update(id portainer.AllowListKey, allowList *portainer.AllowList) error {
|
||||
return service.baseService.Connection.UpdateTx(func(tx portainer.Transaction) error {
|
||||
return service.Tx(tx).Update(id, allowList)
|
||||
})
|
||||
}
|
||||
@@ -1,89 +0,0 @@
|
||||
package allowlist_test
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/datastore"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAllowListReadEmpty(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, ds := datastore.MustNewTestStore(t, false, false)
|
||||
got, err := ds.AllowList().Read(portainer.AllowListSSRF)
|
||||
expected := &portainer.AllowList{
|
||||
ID: portainer.AllowListSSRF,
|
||||
Mode: portainer.SSRFModeOff,
|
||||
Entries: []string{},
|
||||
}
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expected, got)
|
||||
}
|
||||
|
||||
func TestAllowListUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, ds := datastore.MustNewTestStore(t, false, false)
|
||||
|
||||
expected := &portainer.AllowList{
|
||||
ID: portainer.AllowListSSRF,
|
||||
Mode: portainer.SSRFModeEnforce,
|
||||
Entries: []string{"example.com", "10.0.0.0/8"},
|
||||
}
|
||||
|
||||
require.NoError(t, ds.AllowList().Update(portainer.AllowListSSRF, expected))
|
||||
|
||||
got, err := ds.AllowList().Read(portainer.AllowListSSRF)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expected, got)
|
||||
}
|
||||
|
||||
func TestAllowListReadAllEmpty(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, ds := datastore.MustNewTestStore(t, false, false)
|
||||
|
||||
got, err := ds.AllowList().ReadAll()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []portainer.AllowList{}, got)
|
||||
}
|
||||
|
||||
func TestAllowListReadAllAfterUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, ds := datastore.MustNewTestStore(t, false, false)
|
||||
|
||||
expected := portainer.AllowList{
|
||||
ID: portainer.AllowListSSRF,
|
||||
Mode: portainer.SSRFModeEnforce,
|
||||
Entries: []string{"example.com", "10.0.0.0/8"},
|
||||
}
|
||||
|
||||
require.NoError(t, ds.AllowList().Update(portainer.AllowListSSRF, &expected))
|
||||
|
||||
got, err := ds.AllowList().ReadAll()
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []portainer.AllowList{expected}, got)
|
||||
}
|
||||
|
||||
func TestAllowListReadParsedAfterUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, ds := datastore.MustNewTestStore(t, false, false)
|
||||
|
||||
require.NoError(t, ds.AllowList().Update(portainer.AllowListSSRF, &portainer.AllowList{
|
||||
ID: portainer.AllowListSSRF,
|
||||
Mode: portainer.SSRFModeEnforce,
|
||||
Entries: []string{"example.com"},
|
||||
}))
|
||||
|
||||
expected := &portainer.ParsedAllowList{
|
||||
Mode: portainer.SSRFModeEnforce,
|
||||
Nets: []*net.IPNet{},
|
||||
Hosts: map[string]bool{
|
||||
"example.com": true,
|
||||
},
|
||||
}
|
||||
|
||||
got, err := ds.AllowList().ReadParsed(portainer.AllowListSSRF)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, expected, got)
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
package allowlist
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
lru "github.com/hashicorp/golang-lru"
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/dataservices"
|
||||
"github.com/portainer/portainer/pkg/libhttp/ssrf"
|
||||
)
|
||||
|
||||
type ServiceTx struct {
|
||||
baseService dataservices.BaseDataServiceTx[portainer.AllowList, portainer.AllowListKey]
|
||||
cache *lru.Cache
|
||||
}
|
||||
|
||||
func (service *ServiceTx) BucketName() string {
|
||||
return service.baseService.BucketName()
|
||||
}
|
||||
|
||||
func (service *ServiceTx) ReadParsed(id portainer.AllowListKey) (*portainer.ParsedAllowList, error) {
|
||||
allowListAny, ok := service.cache.Get(id)
|
||||
if ok {
|
||||
allowList, ok := allowListAny.(*portainer.ParsedAllowList)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("expected ParsedAllowList in cache but got %T", allowListAny)
|
||||
}
|
||||
|
||||
return allowList, nil
|
||||
}
|
||||
|
||||
allowList, err := service.Read(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
parsed := ssrf.ParseAllowedHosts(allowList.Entries)
|
||||
parsed.Mode = allowList.Mode
|
||||
service.cache.Add(id, &parsed)
|
||||
|
||||
return &parsed, nil
|
||||
}
|
||||
|
||||
func (service *ServiceTx) Read(id portainer.AllowListKey) (*portainer.AllowList, error) {
|
||||
allowList, err := service.baseService.Read(id)
|
||||
if dataservices.IsErrObjectNotFound(err) {
|
||||
allowList = &portainer.AllowList{
|
||||
ID: id,
|
||||
Mode: portainer.SSRFModeOff,
|
||||
Entries: []string{},
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return allowList, nil
|
||||
}
|
||||
|
||||
func (service *ServiceTx) ReadAll() ([]portainer.AllowList, error) {
|
||||
allowLists, err := service.baseService.ReadAll()
|
||||
if err != nil && !dataservices.IsErrObjectNotFound(err) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return allowLists, nil
|
||||
}
|
||||
|
||||
func (service *ServiceTx) Update(id portainer.AllowListKey, allowList *portainer.AllowList) error {
|
||||
if err := service.baseService.Update(id, allowList); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
parsed := ssrf.ParseAllowedHosts(allowList.Entries)
|
||||
parsed.Mode = allowList.Mode
|
||||
service.cache.Add(id, &parsed)
|
||||
return nil
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
package allowlist_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/dataservices"
|
||||
"github.com/portainer/portainer/api/datastore"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestAllowListReadTx(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, ds := datastore.MustNewTestStore(t, false, false)
|
||||
|
||||
var got *portainer.AllowList
|
||||
require.NoError(t, ds.ViewTx(func(tx dataservices.DataStoreTx) error {
|
||||
var err error
|
||||
got, err = tx.AllowList().Read(portainer.AllowListSSRF)
|
||||
return err
|
||||
}))
|
||||
|
||||
expected := &portainer.AllowList{
|
||||
ID: portainer.AllowListSSRF,
|
||||
Mode: portainer.SSRFModeOff,
|
||||
Entries: []string{},
|
||||
}
|
||||
|
||||
require.Equal(t, expected, got)
|
||||
}
|
||||
|
||||
func TestAllowListReadAllEmptyTx(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, ds := datastore.MustNewTestStore(t, false, false)
|
||||
|
||||
var got []portainer.AllowList
|
||||
require.NoError(t, ds.ViewTx(func(tx dataservices.DataStoreTx) error {
|
||||
var err error
|
||||
got, err = tx.AllowList().ReadAll()
|
||||
return err
|
||||
}))
|
||||
|
||||
require.Equal(t, []portainer.AllowList{}, got)
|
||||
}
|
||||
|
||||
func TestAllowListReadAllAfterUpdateTx(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, ds := datastore.MustNewTestStore(t, false, false)
|
||||
|
||||
expected := portainer.AllowList{
|
||||
ID: portainer.AllowListSSRF,
|
||||
Mode: portainer.SSRFModeEnforce,
|
||||
Entries: []string{"example.com"},
|
||||
}
|
||||
|
||||
require.NoError(t, ds.UpdateTx(func(tx dataservices.DataStoreTx) error {
|
||||
return tx.AllowList().Update(portainer.AllowListSSRF, &expected)
|
||||
}))
|
||||
|
||||
var got []portainer.AllowList
|
||||
require.NoError(t, ds.ViewTx(func(tx dataservices.DataStoreTx) error {
|
||||
var err error
|
||||
got, err = tx.AllowList().ReadAll()
|
||||
return err
|
||||
}))
|
||||
|
||||
require.Equal(t, []portainer.AllowList{expected}, got)
|
||||
}
|
||||
|
||||
func TestAllowListUpdateTx(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, ds := datastore.MustNewTestStore(t, false, false)
|
||||
|
||||
expected := &portainer.AllowList{
|
||||
ID: portainer.AllowListSSRF,
|
||||
Mode: portainer.SSRFModeEnforce,
|
||||
Entries: []string{"example.com"},
|
||||
}
|
||||
|
||||
require.NoError(t, ds.UpdateTx(func(tx dataservices.DataStoreTx) error {
|
||||
return tx.AllowList().Update(portainer.AllowListSSRF, expected)
|
||||
}))
|
||||
|
||||
var got *portainer.AllowList
|
||||
require.NoError(t, ds.ViewTx(func(tx dataservices.DataStoreTx) error {
|
||||
var err error
|
||||
got, err = tx.AllowList().Read(portainer.AllowListSSRF)
|
||||
return err
|
||||
}))
|
||||
|
||||
require.Equal(t, expected, got)
|
||||
}
|
||||
@@ -2,10 +2,13 @@ package apikeyrepository
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/dataservices"
|
||||
dserrors "github.com/portainer/portainer/api/dataservices/errors"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// BucketName represents the name of the bucket where this service stores data.
|
||||
@@ -37,10 +40,19 @@ func (service *Service) GetAPIKeysByUserID(userID portainer.UserID) ([]portainer
|
||||
err := service.Connection.GetAll(
|
||||
BucketName,
|
||||
&portainer.APIKey{},
|
||||
dataservices.FilterFn(&result, func(record portainer.APIKey) bool {
|
||||
return record.UserID == userID
|
||||
}),
|
||||
)
|
||||
func(obj any) (any, error) {
|
||||
record, ok := obj.(*portainer.APIKey)
|
||||
if !ok {
|
||||
log.Debug().Str("obj", fmt.Sprintf("%#v", obj)).Msg("failed to convert to APIKey object")
|
||||
return nil, fmt.Errorf("failed to convert to APIKey object: %s", obj)
|
||||
}
|
||||
|
||||
if record.UserID == userID {
|
||||
result = append(result, *record)
|
||||
}
|
||||
|
||||
return &portainer.APIKey{}, nil
|
||||
})
|
||||
|
||||
return result, err
|
||||
}
|
||||
@@ -48,18 +60,27 @@ func (service *Service) GetAPIKeysByUserID(userID portainer.UserID) ([]portainer
|
||||
// GetAPIKeyByDigest returns the API key for the associated digest.
|
||||
// Note: there is a 1-to-1 mapping of api-key and digest
|
||||
func (service *Service) GetAPIKeyByDigest(digest string) (*portainer.APIKey, error) {
|
||||
var found portainer.APIKey
|
||||
|
||||
var k *portainer.APIKey
|
||||
stop := errors.New("ok")
|
||||
err := service.Connection.GetAll(
|
||||
BucketName,
|
||||
&portainer.APIKey{},
|
||||
dataservices.FirstFn(&found, func(key portainer.APIKey) bool {
|
||||
return key.Digest == digest
|
||||
}),
|
||||
)
|
||||
func(obj any) (any, error) {
|
||||
key, ok := obj.(*portainer.APIKey)
|
||||
if !ok {
|
||||
log.Debug().Str("obj", fmt.Sprintf("%#v", obj)).Msg("failed to convert to APIKey object")
|
||||
return nil, fmt.Errorf("failed to convert to APIKey object: %s", obj)
|
||||
}
|
||||
if key.Digest == digest {
|
||||
k = key
|
||||
return nil, stop
|
||||
}
|
||||
|
||||
if errors.Is(err, dataservices.ErrStop) {
|
||||
return &found, nil
|
||||
return &portainer.APIKey{}, nil
|
||||
})
|
||||
|
||||
if errors.Is(err, stop) {
|
||||
return k, nil
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
|
||||
@@ -21,7 +21,7 @@ type mockConnection struct {
|
||||
portainer.Connection
|
||||
}
|
||||
|
||||
func (m mockConnection) UpdateObject(bucket string, key []byte, value any) error {
|
||||
func (m mockConnection) UpdateObject(bucket string, key []byte, value interface{}) error {
|
||||
obj := value.(*testObject)
|
||||
|
||||
m.store[obj.ID] = *obj
|
||||
@@ -50,8 +50,8 @@ func (m mockConnection) ViewTx(fn func(portainer.Transaction) error) error {
|
||||
func (m mockConnection) ConvertToKey(v int) []byte {
|
||||
return []byte(strconv.Itoa(v))
|
||||
}
|
||||
|
||||
func TestReadAll(t *testing.T) {
|
||||
t.Parallel()
|
||||
service := BaseDataService[testObject, int]{
|
||||
Bucket: "testBucket",
|
||||
Connection: mockConnection{store: make(map[int]testObject)},
|
||||
|
||||
@@ -72,13 +72,3 @@ func (service BaseDataServiceTx[T, I]) Delete(ID I) error {
|
||||
identifier := service.Connection.ConvertToKey(int(ID))
|
||||
return service.Tx.DeleteObject(service.Bucket, identifier)
|
||||
}
|
||||
|
||||
func Read[T any](tx portainer.Transaction, bucket string, key []byte) (*T, error) {
|
||||
var element T
|
||||
|
||||
if err := tx.GetObject(bucket, key, &element); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &element, nil
|
||||
}
|
||||
|
||||
@@ -9,8 +9,7 @@ import (
|
||||
)
|
||||
|
||||
func TestCustomTemplateCreate(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, ds := datastore.MustNewTestStore(t, false, false)
|
||||
_, ds := datastore.MustNewTestStore(t, true, false)
|
||||
require.NotNil(t, ds)
|
||||
|
||||
require.NoError(t, ds.CustomTemplate().Create(&portainer.CustomTemplate{ID: 1}))
|
||||
|
||||
@@ -10,8 +10,7 @@ import (
|
||||
)
|
||||
|
||||
func TestCustomTemplateCreateTx(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, ds := datastore.MustNewTestStore(t, false, false)
|
||||
_, ds := datastore.MustNewTestStore(t, true, false)
|
||||
require.NotNil(t, ds)
|
||||
|
||||
require.NoError(t, ds.UpdateTx(func(tx dataservices.DataStoreTx) error {
|
||||
|
||||
@@ -5,18 +5,16 @@ import (
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/database/boltdb"
|
||||
"github.com/portainer/portainer/api/logs"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
var conn portainer.Connection = &boltdb.DbConnection{Path: t.TempDir()}
|
||||
err := conn.Open()
|
||||
require.NoError(t, err)
|
||||
|
||||
defer logs.CloseAndLogErr(conn)
|
||||
defer conn.Close()
|
||||
|
||||
service, err := NewService(conn, func(portainer.Transaction, portainer.EdgeStackID) {})
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
package edgestack
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/dataservices"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
type ServiceTx struct {
|
||||
@@ -21,8 +24,17 @@ func (service ServiceTx) EdgeStacks() ([]portainer.EdgeStack, error) {
|
||||
err := service.tx.GetAll(
|
||||
BucketName,
|
||||
&portainer.EdgeStack{},
|
||||
dataservices.AppendFn(&stacks),
|
||||
)
|
||||
func(obj any) (any, error) {
|
||||
stack, ok := obj.(*portainer.EdgeStack)
|
||||
if !ok {
|
||||
log.Debug().Str("obj", fmt.Sprintf("%#v", obj)).Msg("failed to convert to EdgeStack object")
|
||||
return nil, fmt.Errorf("failed to convert to EdgeStack object: %s", obj)
|
||||
}
|
||||
|
||||
stacks = append(stacks, *stack)
|
||||
|
||||
return &portainer.EdgeStack{}, nil
|
||||
})
|
||||
|
||||
return stacks, err
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package edgestackstatus
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/dataservices"
|
||||
)
|
||||
@@ -87,9 +85,5 @@ func (s *Service) Clear(edgeStackID portainer.EdgeStackID, relatedEnvironmentsID
|
||||
}
|
||||
|
||||
func (s *Service) key(edgeStackID portainer.EdgeStackID, endpointID portainer.EndpointID) []byte {
|
||||
k := make([]byte, 16)
|
||||
binary.BigEndian.PutUint64(k[:8], uint64(edgeStackID))
|
||||
binary.BigEndian.PutUint64(k[8:], uint64(endpointID))
|
||||
|
||||
return k
|
||||
return append(s.conn.ConvertToKey(int(edgeStackID)), s.conn.ConvertToKey(int(endpointID))...)
|
||||
}
|
||||
|
||||
@@ -119,19 +119,6 @@ func (service *Service) Endpoints() ([]portainer.Endpoint, error) {
|
||||
return endpoints, nil
|
||||
}
|
||||
|
||||
// ReadAll retrieves all the elements that satisfy all the provided predicates.
|
||||
func (service *Service) ReadAll(predicates ...func(endpoint portainer.Endpoint) bool) ([]portainer.Endpoint, error) {
|
||||
var endpoints []portainer.Endpoint
|
||||
var err error
|
||||
|
||||
err = service.connection.ViewTx(func(tx portainer.Transaction) error {
|
||||
endpoints, err = service.Tx(tx).ReadAll(predicates...)
|
||||
return err
|
||||
})
|
||||
|
||||
return endpoints, err
|
||||
}
|
||||
|
||||
// EndpointIDByEdgeID returns the EndpointID from the given EdgeID using an in-memory index
|
||||
func (service *Service) EndpointIDByEdgeID(edgeID string) (portainer.EndpointID, bool) {
|
||||
service.mu.RLock()
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user