haraka-plugin-karma 2.0.0 → 2.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,27 +11,29 @@ jobs:
11
11
  uses: haraka/.github/.github/workflows/lint.yml@master
12
12
 
13
13
  test:
14
+ needs: lint
14
15
  runs-on: ${{ matrix.os }}
15
16
  services:
16
17
  redis:
17
18
  image: redis
18
19
  ports:
19
- - 6379/tcp
20
+ - 6379:6379
20
21
  strategy:
21
22
  matrix:
22
23
  os: [ ubuntu-latest ]
23
24
  node-version: [ 14, 16, 18 ]
24
25
  fail-fast: false
25
26
  steps:
26
- - uses: actions/checkout@v2
27
+ - uses: actions/checkout@v3
27
28
  - uses: actions/setup-node@v3
28
- name: Node.js ${{ matrix.node-version }}
29
+ name: Node ${{ matrix.node-version }} on ${{ matrix.os }}
29
30
  with:
30
31
  node-version: ${{ matrix.node-version }}
31
32
  - run: npm install
32
33
  - run: npm test
33
34
 
34
35
  test-win:
36
+ needs: lint
35
37
  runs-on: ${{ matrix.os }}
36
38
  strategy:
37
39
  matrix:
@@ -41,7 +43,7 @@ jobs:
41
43
  steps:
42
44
  - uses: actions/checkout@v3
43
45
  - uses: actions/setup-node@v3
44
- name: Use Node.js ${{ matrix.node-version }}
46
+ name: Node ${{ matrix.node-version }} on ${{ matrix.os }}
45
47
  with:
46
48
  node-version: ${{ matrix.node-version }}
47
49
  - run: npm install
@@ -0,0 +1,14 @@
1
+ name: CodeQL
2
+
3
+ on:
4
+ push:
5
+ branches: [ master ]
6
+ pull_request:
7
+ # The branches below must be a subset of the branches above
8
+ branches: [ master ]
9
+ schedule:
10
+ - cron: '18 7 * * 4'
11
+
12
+ jobs:
13
+ codeql:
14
+ uses: haraka/.github/.github/workflows/codeql.yml@master
@@ -4,6 +4,8 @@ on:
4
4
  push:
5
5
  branches:
6
6
  - master
7
+ paths:
8
+ - package.json
7
9
 
8
10
  env:
9
11
  CI: true
@@ -12,18 +14,3 @@ jobs:
12
14
  publish:
13
15
  uses: haraka/.github/.github/workflows/publish.yml@master
14
16
  secrets: inherit
15
-
16
- publish-gpr:
17
- runs-on: ubuntu-latest
18
- permissions:
19
- contents: read
20
- packages: write
21
- steps:
22
- - uses: actions/checkout@v3
23
- - uses: actions/setup-node@v3
24
- with:
25
- node-version: 16
26
- registry-url: https://npm.pkg.github.com/
27
- - run: npm publish
28
- env:
29
- NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
@@ -0,0 +1,40 @@
1
+ #!/bin/sh
2
+
3
+ branch_is_master()
4
+ {
5
+ if [ "$(git branch --show-current)" = "master" ]; then
6
+ return 0
7
+ fi
8
+
9
+ return 1
10
+ }
11
+
12
+ repo_is_clean()
13
+ {
14
+ if [ -z "$(git status --porcelain)" ]; then
15
+ return 0
16
+ fi
17
+
18
+ return 1
19
+ }
20
+
21
+ assure_repo_is_clean()
22
+ {
23
+ if repo_is_clean; then return 0; fi
24
+
25
+ echo
26
+ echo "ERROR: Uncommitted changes, cowardly refusing to continue..."
27
+ echo
28
+ sleep 2
29
+
30
+ git status
31
+
32
+ return 1
33
+ }
34
+
35
+ find_changelog()
36
+ {
37
+ CHANGELOG=$(ls [Cc][Hh][Aa]*.md)
38
+ export CHANGELOG
39
+ #echo "I found your CHANGELOG at: $CHANGELOG"
40
+ }
package/.release/do.sh ADDED
@@ -0,0 +1,51 @@
1
+ #!/bin/sh
2
+
3
+ usage() {
4
+ echo "do.sh {major | minor | patch}"
5
+ exit
6
+ }
7
+
8
+ . .release/base.sh || exit
9
+
10
+ case "$1" in
11
+ "major" )
12
+ ;;
13
+ "minor" )
14
+ ;;
15
+ "patch" )
16
+ ;;
17
+ *)
18
+ usage
19
+ ;;
20
+ esac
21
+
22
+ NEW_VERSION=$(npm --no-git-tag-version version "$1")
23
+
24
+ YMD=$(date "+%Y-%m-%d")
25
+ # echo "Preparing $NEW_VERSION - $YMD"
26
+
27
+ if branch_is_master; then
28
+ git checkout -b "release-${NEW_VERSION}"
29
+ fi
30
+
31
+ update_changes() {
32
+ tee .release/new.txt <<EO_CHANGE
33
+
34
+
35
+ #### ${NEW_VERSION//v} - $YMD
36
+
37
+ -
38
+ -
39
+ EO_CHANGE
40
+
41
+ sed -i '' -e "/#### N.N.N.*$/r .release/new.txt" "$CHANGELOG"
42
+ rm .release/new.txt
43
+
44
+ if command -v open; then open "$CHANGELOG"; fi
45
+
46
+ echo
47
+ echo "AFTER editing $CHANGELOG, run: .release/push.sh"
48
+ }
49
+
50
+ find_changelog
51
+ update_changes
@@ -0,0 +1,23 @@
1
+ #!/bin/sh
2
+
3
+ . .release/base.sh || exit
4
+
5
+ if branch_is_master; then
6
+ echo "ERROR: run the release scripts in a feature branch! (not master)"
7
+ exit
8
+ fi
9
+
10
+ VERSION=$(node -e 'console.log(require("./package.json").version)')
11
+
12
+ find_changelog
13
+
14
+ git add package.json
15
+ git add "$CHANGELOG"
16
+
17
+ git commit -m "Release v$VERSION"
18
+
19
+ git push --set-upstream origin "$(git branch --show-current)"
20
+
21
+ if command -v gh; then
22
+ gh pr create
23
+ fi
package/Changes.md CHANGED
@@ -1,80 +1,94 @@
1
1
 
2
+ #### N.N.N - YYYY-MM-DD
2
3
 
3
- ## 2.0.0 - 2022-03-29
4
+
5
+ #### 2.0.3 - 2022-05-28
6
+
7
+ - fix: depend directly on redis
8
+ - fix: update redis command names for v4 compatibility
9
+ - fix: update redis commands to be async
10
+
11
+
12
+ #### 2.0.1 - 2022-05-27
13
+
14
+ - chore(ci): depend on shared GHA workflows
15
+
16
+
17
+ #### 2.0.0 - 2022-03-29
4
18
 
5
19
  - remove lots of plugin=this
6
20
  - remove unnecessary braces and trailing ;
7
21
  - some promises.
8
22
 
9
23
 
10
- ## 1.0.14 - 2022-02-14
24
+ #### 1.0.14 - 2022-02-14
11
25
 
12
26
  - try to unsubscribe in case connection is marked to skip during transaction
13
27
 
14
28
 
15
- ## 1.0.13 - 2019-04-23
29
+ #### 1.0.13 - 2019-04-23
16
30
 
17
31
  - add 'exists' pattern
18
32
 
19
33
 
20
- ## 1.0.12 - 2019-03-08
34
+ #### 1.0.12 - 2019-03-08
21
35
 
22
- - don't interfere with STARTLS and AUTH when karmi is listed above those plugins in config/plugins
36
+ - don't interfere with STARTLS and AUTH when karma is listed above those plugins in config/plugins
23
37
 
24
38
 
25
- ## 1.0.11 - 2017-10-25
39
+ #### 1.0.11 - 2017-10-25
26
40
 
27
41
  - private addresses and flagged connections exemption
28
42
 
29
43
 
30
- ## 1.0.10 - 2017-08-30
44
+ #### 1.0.10 - 2017-08-30
31
45
 
32
46
  - add TLS awards #19
33
47
 
34
48
 
35
- ## 1.0.9 - 2017-07-29
49
+ #### 1.0.9 - 2017-07-29
36
50
 
37
51
  - splash on some es6
38
52
  - add AppVeyor CI testing
39
53
 
40
54
 
41
- ## 1.0.8 - 2017-06-26
55
+ #### 1.0.8 - 2017-06-26
42
56
 
43
57
  - revert #9, it breaks current Haraka deployments
44
58
 
45
59
 
46
- ## 1.0.7 - 2017-06-16
60
+ #### 1.0.7 - 2017-06-16
47
61
 
48
62
  - update for eslint 4 compat
49
63
  - Add results_redis_publish=true for haraka-results changes #9
50
64
 
51
65
 
52
- ## 1.0.6 - 2017-05-04
66
+ #### 1.0.6 - 2017-05-04
53
67
 
54
68
  - emit error if redis plugin didn't create connection
55
69
 
56
70
 
57
- ## 1.0.5 - 2017-02-06
71
+ #### 1.0.5 - 2017-02-06
58
72
 
59
73
  - move merge_redis_ini into load_karma_ini, so it also gets applied
60
74
  after a karma.ini change
61
75
  - skip redis operations when no connection exists
62
76
 
63
77
 
64
- ## 1.0.4 - 2017-01-29
78
+ #### 1.0.4 - 2017-01-29
65
79
 
66
80
  - use the new haraka-plugin-redis
67
81
  - remove exceptions for soft denials. This makes denial time simpler.
68
82
  - rules updates
69
83
 
70
84
 
71
- ## 1.0.3 - 2017-01-27
85
+ #### 1.0.3 - 2017-01-27
72
86
 
73
87
  - add rule #280 for known-senders
74
88
  - add support for 'length' type, with eq, gt, and lt operators
75
89
  - use shared haraka-eslint
76
90
 
77
91
 
78
- ## 1.0.2 - 2017-01-24
92
+ #### 1.0.2 - 2017-01-24
79
93
 
80
94
  - use redis.merge_redis_ini()
package/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
  // karma - reward good and penalize bad mail senders
3
3
 
4
4
  const constants = require('haraka-constants')
5
+ const redis = require('redis')
5
6
  const utils = require('haraka-utils')
6
7
 
7
8
  const phase_prefixes = utils.to_object([
@@ -104,8 +105,16 @@ exports.results_init = async function (next, connection) {
104
105
 
105
106
  if (!this.result_awards) return next() // not configured
106
107
 
107
- // subscribe to result_store publish messages
108
- await this.redis_subscribe(connection, (channel, message) => {
108
+ if (connection.notes.redis) {
109
+ connection.logdebug(this, `redis already subscribed`)
110
+ return // another plugin has already called this.
111
+ }
112
+
113
+ connection.notes.redis = redis.createClient(this.redisCfg.pubsub)
114
+ await connection.notes.redis.connect()
115
+
116
+ const pattern = this.get_redis_sub_channel(connection)
117
+ connection.notes.redis.pSubscribe(pattern, (message) => {
109
118
  this.check_result(connection, message)
110
119
  })
111
120
 
@@ -543,22 +552,18 @@ exports.ip_history_from_redis = function (next, connection) {
543
552
  // redis plugin is emitting errors, no need to here
544
553
  if (!plugin.db) return next()
545
554
 
546
- plugin.db.hgetall(dbkey, (err, dbr) => {
547
- if (err) {
548
- connection.results.add(plugin, { err })
549
- return next()
550
- }
551
-
555
+ plugin.db.hGetAll(dbkey).then(dbr => {
552
556
  if (dbr === null) {
553
557
  plugin.init_ip(dbkey, connection.remote.ip, expire)
554
558
  return next()
555
559
  }
556
560
 
557
561
  plugin.db.multi()
558
- .hincrby(dbkey, 'connections', 1) // increment total conn
562
+ .hIncrBy(dbkey, 'connections', 1) // increment total conn
559
563
  .expire(dbkey, expire) // extend expiration
560
- .exec((err2, replies) => {
561
- if (err2) connection.results.add(plugin, {err: err2})
564
+ .exec()
565
+ .catch(err => {
566
+ connection.results.add(plugin, { err })
562
567
  })
563
568
 
564
569
  const results = {
@@ -580,8 +585,13 @@ exports.ip_history_from_redis = function (next, connection) {
580
585
  connection.results.add(plugin, results)
581
586
 
582
587
  plugin.check_awards(connection)
583
- return next()
588
+ next()
584
589
  })
590
+ .catch(err => {
591
+ connection.results.add(plugin, { err })
592
+ next()
593
+ })
594
+
585
595
  }
586
596
 
587
597
  exports.hook_mail = function (next, connection, params) {
@@ -665,10 +675,10 @@ exports.increment = function (connection, key, val) {
665
675
  const plugin = this
666
676
  if (!plugin.db) return
667
677
 
668
- plugin.db.hincrby(`karma|${connection.remote.ip}`, key, 1)
678
+ plugin.db.hIncrBy(`karma|${connection.remote.ip}`, key, 1)
669
679
 
670
680
  const asnkey = plugin.get_asn_key(connection)
671
- if (asnkey) plugin.db.hincrby(asnkey, key, 1)
681
+ if (asnkey) plugin.db.hIncrBy(asnkey, key, 1)
672
682
  }
673
683
 
674
684
  exports.hook_disconnect = function (next, connection) {
@@ -930,19 +940,14 @@ exports.check_asn = function (connection, asnkey) {
930
940
 
931
941
  if (this.cfg.asn.report_as) report_as.name = this.cfg.asn.report_as
932
942
 
933
- this.db.hgetall(asnkey, (err, res) => {
934
- if (err) {
935
- connection.results.add(this, { err })
936
- return
937
- }
938
-
943
+ this.db.hGetAll(asnkey).then(res => {
939
944
  if (res === null) {
940
945
  const expire = (this.cfg.redis.expire_days || 60) * 86400 // days
941
946
  this.init_asn(asnkey, expire)
942
947
  return
943
948
  }
944
949
 
945
- this.db.hincrby(asnkey, 'connections', 1)
950
+ this.db.hIncrBy(asnkey, 'connections', 1)
946
951
  const asn_score = parseInt(res.good || 0) - (res.bad || 0)
947
952
  const asn_results = {
948
953
  asn_score,
@@ -970,12 +975,15 @@ exports.check_asn = function (connection, asnkey) {
970
975
 
971
976
  connection.results.add(report_as, asn_results)
972
977
  })
978
+ .catch(err => {
979
+ connection.results.add(this, { err })
980
+ })
973
981
  }
974
982
 
975
- exports.init_ip = function (dbkey, rip, expire) {
983
+ exports.init_ip = async function (dbkey, rip, expire) {
976
984
  if (!this.db) return
977
- this.db.multi()
978
- .hmset(dbkey, {'bad': 0, 'good': 0, 'connections': 1})
985
+ await this.db.multi()
986
+ .hmSet(dbkey, {'bad': 0, 'good': 0, 'connections': 1})
979
987
  .expire(dbkey, expire)
980
988
  .exec()
981
989
  }
@@ -992,7 +1000,7 @@ exports.init_asn = function (asnkey, expire) {
992
1000
  const plugin = this
993
1001
  if (!plugin.db) return
994
1002
  plugin.db.multi()
995
- .hmset(asnkey, {'bad': 0, 'good': 0, 'connections': 1})
1003
+ .hmSet(asnkey, {'bad': 0, 'good': 0, 'connections': 1})
996
1004
  .expire(asnkey, expire * 2) // keep ASN longer
997
1005
  .exec()
998
1006
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "haraka-plugin-karma",
3
- "version": "2.0.0",
3
+ "version": "2.0.3",
4
4
  "description": "A heuristics scoring and reputation engine for SMTP connections",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -26,7 +26,8 @@
26
26
  "address-rfc2821": "*",
27
27
  "haraka-constants": ">=1.0.2",
28
28
  "haraka-utils": "*",
29
- "haraka-plugin-redis": "2"
29
+ "haraka-plugin-redis": "2",
30
+ "redis": "4"
30
31
  },
31
32
  "devDependencies": {
32
33
  "eslint": "8",
package/test/karma.js CHANGED
@@ -762,7 +762,7 @@ describe('tls', function () {
762
762
  })
763
763
  })
764
764
 
765
- describe('skiping_hooks', function () {
765
+ describe('skipping hooks', function () {
766
766
  beforeEach(_set_up)
767
767
 
768
768
  it('notes.disable_karma', function (done) {