GithubHelp home page GithubHelp logo

mozilla-services / ip-reputation-js-client Goto Github PK

View Code? Open in Web Editor NEW
5.0 15.0 8.0 880 KB

A node JS client to the iprepd IP reputation service

License: Mozilla Public License 2.0

JavaScript 96.61% Shell 2.16% Dockerfile 1.23%

ip-reputation-js-client's Introduction

iprepd (IP Reputation Service) node.js client library

Client library to send object reputations to the iprepd service.

npm version Coverage Status CircleCI

Overview

iprepd is a service that supports storing and retrieving reputations associated with various object types, the most common being IP addresses but including others such as account names and email addresses. This library can be used by Node applications to integrate directly with this API.

Usage

Functions

Create a client:

const IPReputationClient = require('ip-reputation-service-client-js')

const client = new IPReputationClient({
    serviceUrl: 'http://<iprepd service host without trailing slash>',
    id: '<a hawk ID>',
    key: '<a hawk key>',
    timeout: <number in ms>
})

Get the reputation for an IP:

client.getTyped('ip', '127.0.0.1').then(function (response) {
    if (response && response.statusCode === 404) {
        console.log('No reputation found for 127.0.0.1');
    } else {
        console.log('127.0.0.1 has reputation: ', response.body.reputation);
    }
});

Set the reputation for an IP:

client.updateTyped('ip', '127.0.0.1', 79).then(function (response) {
    console.log('Set reputation for 127.0.0.1 to 79.');
});

Remove an IP:

client.removeTyped('ip', '127.0.0.1').then(function (response) {
    console.log('Removed reputation for 127.0.0.1.');
});

Send a violation for an IP:

client.sendViolationTyped('ip', '127.0.0.1', 'exceeded-password-reset-failure-rate-limit').then(function (response) {
    console.log('Applied violation to 127.0.0.1.');
});

Legacy functions

Previous versions of iprepd only supported IP addresses; these functions remain as a compatibility layer for applications that still make use of them, and are essentially wrappers around the typed function calls.

Get the reputation for an IP:

client.get('127.0.0.1').then(function (response) {
    if (response && response.statusCode === 404) {
        console.log('No reputation found for 127.0.0.1');
    } else {
        console.log('127.0.0.1 has reputation: ', response.body.reputation);
    }
});

Set the reputation for an IP:

client.update('127.0.0.1', 79).then(function (response) {
    console.log('Set reputation for 127.0.0.1 to 79.');
});

Remove an IP:

client.remove('127.0.0.1').then(function (response) {
    console.log('Removed reputation for 127.0.0.1.');
});

Send a violation for an IP:

client.sendViolation('127.0.0.1', 'exceeded-password-reset-failure-rate-limit').then(function (response) {
    console.log('Applied violation to 127.0.0.1.');
});

Development

Tests run against the iprepd service with docker-compose from the ip-reputation-js-client repo root:

  1. Install docker and docker-compose
  2. Run docker-compose build.
  3. Run docker-compose run --rm test npm install to collect package dependencies.
  4. Run docker-compose run --rm test to test.
  5. Open coverage/lcov-report/index.html to see the coverage report
  6. Run docker-compose down when you are finished running tests to remove cache and web containers.

ip-reputation-js-client's People

Contributors

ajvb avatar dependabot[bot] avatar g-k avatar mozilla-github-standards avatar pdehaan avatar vladikoff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ip-reputation-js-client's Issues

Change use of promisify of `request` and `request`.defaults to work around a newrelic bug

Hey @g-k,

You might not access the first two of these, but we ran into a bug with using newrelic + bluebird.

... but it would useful if you changed the reference to request.defaults like this:

const _request = require('request'));
const request = Promise.promisify(_request);

and later call .defaults on the _request object at https://github.com/mozilla-services/ip-reputation-js-client/blob/master/lib/client.js#L43.

Ping me on irc or here if you have questions.

Travis CI free usage ends Dec 3; mozilla repos should switch to other CI platforms

We're opening this issue because your project has used Travis CI within the last 6 months. If you have already migrated off it, you can close and ignore this issue.

Travis CI is ending free builds on public repositories. travis-ci.com stopped providingthem in early November, and travis-ci.org will stop after December 31, 2020. To avoid disruptions to your workflows, you must migrate to another CI service.

For production use cases, we recommend switching to CircleCI. This service is already widely used within Mozilla. There is a guide to migrating from Travis CI to CircleCI available here.

For non production use cases, we recommend either CircleCI or Github Actions. There is a guide to migrating from Travis CI to Github Actions available here. Github Actions usage within Mozilla is new, and you will have to work with our github administrators to enable specific actions following this process.

If you have any questions, reach out in #github-admin:mozilla.org on matrix.

Test without service running

Add ability to test against mocked service without running tigerblood (but still be able to integration test against a running instance too).

Update to remove usage of legacy iprepd reputation endpoints

This module currently makes use of the legacy untyped reputation endpoints.

It should be updated in a fashion that does not break downstream consumers to support the current endpoints. This will allow us to deprecate the legacy endpoint support in iprepd.

Task "validate-shrinkwrap" not found

Steps to reproduce:

  1. Clone this repo using git clone [email protected]:mozilla-services/ip-reputation-js-client.git.
  2. Run $ npm install to install dependencies.
  3. Run $ grunt to run the default Grunt task:
    grunt.registerTask('default', ['lint', 'copyright', 'validate-shrinkwrap'])

Actual results:

$ grunt
Warning: Task "validate-shrinkwrap" not found. Use --force to continue.

Aborted due to warnings.

$ echo $? # 3

CODE_OF_CONDUCT.md file missing

As of January 1 2019, Mozilla requires that all GitHub projects include this CODE_OF_CONDUCT.md file in the project root. The file has two parts:

  1. Required Text - All text under the headings Community Participation Guidelines and How to Report, are required, and should not be altered.
  2. Optional Text - The Project Specific Etiquette heading provides a space to speak more specifically about ways people can work effectively and inclusively together. Some examples of those can be found on the Firefox Debugger project, and Common Voice. (The optional part is commented out in the raw template file, and will not be visible until you modify and uncomment that part.)

If you have any questions about this file, or Code of Conduct policies and procedures, please reach out to [email protected].

(Message COC001)

[bug 1475698] apply timeout to connection and DNS lookup timeouts for new connections

The current timeout param only applies to waiting between reads and writes on the socket.

New connections that do not reuse an existing TCP connection, will fire lookup and connect events, but potentially hit the OS connection (and probably DNS lookup) timeouts.

per irc chat w/ @ameihm0912:

one of the tests we did was to totally firewall off the iprepd elb
if we do that, the request ends up taking 30s or so (reverts to default OS timeout per requests documentation)

The docs say:

Note that if the underlying TCP connection cannot be established, the OS-wide TCP connection timeout will overrule the timeout option (the default in Linux can be anywhere from 20-120 seconds).

Setting this at the OS-level with net.ipv4.tcp_syn_retries is not desirable since we want other FxA connections to use the default timeout.

The easiest solution might be to:

  • add a setTimeout(abort.bind(null, request), timeout_param) to each request where abort:

Additional info in: https://github.com/request/request/tree/master#timeouts
The commit adding connection timeout tracking is: request/request@ee4cfbb

Audit which files get published to npm

Looks like we are publishing oodles of files to npm, and I don't think a lot of them are necessary (ie: grunt tasks, etc).


$ npm init -y && npm i ip-reputation-js-client -D
Wrote to /Users/pdehaan/dev/tmp/del/ip-rep-test/package.json:

{
  "name": "ip-rep-test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "Peter deHaan <[email protected]> (https://about.me/peterdehaan)",
  "license": "WTFPL"
}

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN [email protected] No description
npm WARN [email protected] No repository field.

+ [email protected]
added 62 packages in 3.567s

$ ls -lashR node_modules/ip-reputation-js-client
8 -rw-r--r--   1 pdehaan  staff   450B Oct 26  1985 Gruntfile.js
8 -rw-r--r--   1 pdehaan  staff   2.0K Oct 26  1985 README.md
0 drwxr-xr-x   7 pdehaan  staff   224B May 14 15:53 grunttasks
0 drwxr-xr-x   3 pdehaan  staff    96B May 14 15:53 lib
8 -rw-r--r--   1 pdehaan  staff   2.0K May 14 15:53 package.json
0 drwxr-xr-x   5 pdehaan  staff   160B May 14 15:53 scripts
0 drwxr-xr-x   3 pdehaan  staff    96B May 14 15:53 test

node_modules/ip-reputation-js-client/grunttasks:
8 -rw-r--r--  1 pdehaan  staff   789B Oct 26  1985 bump.js
8 -rw-r--r--  1 pdehaan  staff   700B Oct 26  1985 copyright.js
8 -rw-r--r--  1 pdehaan  staff   421B Oct 26  1985 eslint.js
8 -rw-r--r--  1 pdehaan  staff   364B Oct 26  1985 nsp.js
8 -rw-r--r--  1 pdehaan  staff   857B Oct 26  1985 version.js

node_modules/ip-reputation-js-client/lib:
16 -rw-r--r--  1 pdehaan  staff   4.6K Oct 26  1985 client.js

node_modules/ip-reputation-js-client/scripts:
8 -rwxr-xr-x  1 pdehaan  staff   783B Oct 26  1985 cleanup-test-db.sh
8 -rwxr-xr-x  1 pdehaan  staff   1.2K Oct 26  1985 setup-test-db.sh
8 -rwxr-xr-x  1 pdehaan  staff   412B Oct 26  1985 test-local.sh

node_modules/ip-reputation-js-client/test/local:
16 -rw-r--r--  1 pdehaan  staff   5.5K Oct 26  1985 reputation_service_client_tests.js

I don't know what file(s) are required for any of this to work, if it's only "lib/client.js" (per package.json's main attribute), or if we'd need others.
Not like any of these files are huge, but keeping it as slim as possible is probably a good idea.

Missing .eslintrc type file

Currently it looks like ESLint passes with zero errors.

$ grunt lint
Running "eslint:files" (eslint) task

Done.

... Sadly, it looks like the files all lint because there is no .eslintrc.js (or .json, or .yml) file, and the default config has no rules.

This is probably a bare minimum config:

module.exports = {
  env: {
    es6: true,
    node: true
  },
  extends: [
    'eslint:recommended',
    'fxa' // per "eslint-config-fxa", this would probably need to switch to "plugin:fxa/client" if you wanted to switch to "eslint-plugin-fxa".
  ],
  root: true,
  rules: {
    'strict': 'off'
  }
};

"eslint-config-fxa": "2.1.0",

Current output:

$ grunt lint
Running "eslint:files" (eslint) task

/Users/pdehaan/dev/github/mozilla-services/ip-reputation-js-client/lib/client.js
   72:37  error  Strings must use singlequote  quotes
   93:37  error  Strings must use singlequote  quotes
  113:37  error  Strings must use singlequote  quotes
  132:37  error  Strings must use singlequote  quotes
  152:37  error  Strings must use singlequote  quotes

/Users/pdehaan/dev/github/mozilla-services/ip-reputation-js-client/test/local/reputation_service_client_tests.js
  14:32  error  Strings must use singlequote  quotes

✖ 6 problems (6 errors, 0 warnings)

Warning: Task "eslint:files" failed. Use --force to continue.

Aborted due to warnings.

$ echo $? # 3

The output is slightly different if you run ESLint directly, so you may also need to tweak the glob pattern in grunttasks/eslint.js to scan the *.js files in the root directory:

files: [
'{bin/,lib/**/,grunttasks/,scripts/,test/**/}*.js'
]

% $(npm bin)/eslint .

/Users/pdehaan/dev/github/mozilla-services/ip-reputation-js-client/Gruntfile.js
   6:15  error  Missing semicolon  semi
   8:37  error  Missing semicolon  semi
  10:32  error  Missing semicolon  semi
  12:78  error  Missing semicolon  semi
  13:41  error  Missing semicolon  semi
  14:2   error  Missing semicolon  semi

/Users/pdehaan/dev/github/mozilla-services/ip-reputation-js-client/lib/client.js
   72:37  error  Strings must use singlequote  quotes
   93:37  error  Strings must use singlequote  quotes
  113:37  error  Strings must use singlequote  quotes
  132:37  error  Strings must use singlequote  quotes
  152:37  error  Strings must use singlequote  quotes

/Users/pdehaan/dev/github/mozilla-services/ip-reputation-js-client/test/local/reputation_service_client_tests.js
  14:32  error  Strings must use singlequote  quotes

✖ 12 problems (12 errors, 0 warnings)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.