Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate(W-14169383): spaces: Upgrade trusted-ips:remove #2868

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions packages/cli/src/commands/spaces/trusted-ips/remove.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import color from '@heroku-cli/color'
import {Command, flags} from '@heroku-cli/command'
import {Args, ux} from '@oclif/core'
import * as Heroku from '@heroku-cli/schema'
import heredoc from 'tsheredoc'

export default class Remove extends Command {
static aliases = ['trusted-ips:remove']
static topic = 'trusted-ips'
static description = heredoc(`
Remove a range from the list of trusted IP ranges
Uses CIDR notation.`)

static examples = [heredoc(`
$ heroku trusted-ips:remove --space my-space 192.168.2.0/24
Removed 192.168.2.0/24 from trusted IP ranges on my-space
`)]

static flags = {
space: flags.string({optional: false, description: 'space to remove rule from'}),
confirm: flags.string({description: 'set to space name to bypass confirm prompt'}),
}

static args = {
source: Args.string({required: true}),
}

public async run(): Promise<void> {
const {flags, args} = await this.parse(Remove)
const space = flags.space
const url = `/spaces/${space}/inbound-ruleset`
const opts = {headers: {Accept: 'application/vnd.heroku+json; version=3.dogwood'}}
const {body: rules} = await this.heroku.get<Heroku.InboundRuleset>(url, opts)
if (rules.rules?.length === 0) {
throw new Error('No IP ranges are configured. Nothing to do.')
}

const originalLength = rules.rules?.length
rules.rules = rules.rules?.filter(r => r.source !== args.source)
if (rules.rules?.length === originalLength) {
throw new Error(`No IP range matching ${args.source} was found.`)
}

await this.heroku.put(url, {...opts, body: rules})
ux.log(`Removed ${color.cyan.bold(args.source)} from trusted IP ranges on ${color.cyan.bold(space)}`)
ux.warn('It may take a few moments for the changes to take effect.')
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {expect} from '@oclif/test'
import * as nock from 'nock'
import {stdout} from 'stdout-stderr'
import heredoc from 'tsheredoc'
import Cmd from '../../../../../src/commands/spaces/trusted-ips/remove'
import runCommand from '../../../../helpers/runCommand'

describe('trusted-ips:remove', function () {
it('removes a CIDR entry from the trusted IP ranges', async function () {
const api = nock('https://api.heroku.com:443')
.get('/spaces/my-space/inbound-ruleset')
.reply(200, {
created_by: 'dickeyxxx',
rules: [
{source: '128.0.0.1/20', action: 'allow'},
{source: '127.0.0.1/20', action: 'allow'},
],
},
)
.put('/spaces/my-space/inbound-ruleset', {
created_by: 'dickeyxxx',
rules: [
{source: '128.0.0.1/20', action: 'allow'},
],
})
.reply(200, {rules: []})
await runCommand(Cmd, ['127.0.0.1/20', '--space', 'my-space'])
expect(stdout.output).to.eq(heredoc(`
Removed 127.0.0.1/20 from trusted IP ranges on my-space
`))
api.done()
})
})
41 changes: 0 additions & 41 deletions packages/spaces/commands/trusted-ips/remove.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/spaces/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ exports.commands = [
require('./commands/vpn/update'),
require('./commands/drains/get'),
require('./commands/trusted-ips'),
require('./commands/trusted-ips/remove'),
]

This file was deleted.