-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Omer Demirok <odemirok@chef.io>
- Loading branch information
Omer Demirok
authored and
Stuart Paterson
committed
Sep 28, 2020
1 parent
6bd8953
commit 8bd8f61
Showing
15 changed files
with
385 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
--- | ||
title: About the azure_subscription Resource | ||
platform: azure | ||
--- | ||
|
||
# azure_subscription | ||
|
||
Use the `azure_subscription` InSpec audit resource to test properties of the current subscription. | ||
|
||
## Azure REST API version, endpoint and http client parameters | ||
|
||
This resource interacts with api versions supported by the resource provider. | ||
The `api_version` can be defined as a resource parameter. | ||
If not provided, the latest version will be used. | ||
For more information, refer to [`azure_generic_resource`](azure_generic_resource.md). | ||
|
||
Unless defined, `azure_cloud` global endpoint, and default values for the http client will be used. | ||
For more information, refer to the resource pack [README](../../README.md). | ||
|
||
## Availability | ||
|
||
### Installation | ||
|
||
This resource is available in the [InSpec Azure resource pack](https://github.com/inspec/inspec-azure). | ||
For an example `inspec.yml` file and how to set up your Azure credentials, refer to resource pack [README](../../README.md#Service-Principal). | ||
|
||
## Syntax | ||
|
||
This resource will retrieve the current subscription id that InSpec uses unless it is provided via `id` or `resource_id` parameters. | ||
```ruby | ||
describe azure_subscription do | ||
it { should exist } | ||
end | ||
``` | ||
or | ||
```ruby | ||
describe azure_subscription(id: '2e0b423p-aaaa-bbbb-1111-ee558463aabbd') do | ||
it { should exist } | ||
end | ||
``` | ||
or | ||
```ruby | ||
describe azure_subscription(resource_id: '/subscriptions/2e0b423p-aaaa-bbbb-1111-ee558463aabbd') do | ||
it { should exist } | ||
end | ||
``` | ||
## Parameters | ||
|
||
| Name | Description | | ||
|---------------------------------------|-------------| | ||
| id | The ID of the target subscription. `2e0b423p-aaaa-bbbb-1111-ee558463aabbd` | | ||
| resource_id | The fully qualified ID for the subscription. `/subscriptions/2e0b423p-aaaa-bbbb-1111-ee558463aabbd` | | ||
| locations_api_version | The endpoint api version for the `locations` property. Optional. The latest version will be used unless provided. | | ||
|
||
## Properties | ||
|
||
| Property | Description | | ||
|---------------------------|-------------| | ||
| name | The subscription display name. | | ||
| id | The subscription ID. `2e0b423p-aaaa-bbbb-1111-ee558463aabbd` | | ||
| locations | The list of all available geo-locations. | | ||
| managedByTenants | An array containing the [tenants](https://docs.microsoft.com/en-us/rest/api/resources/subscriptions/get#managedbytenant) managing the subscription. | | ||
|
||
For properties applicable to all resources, such as `type`, `properties`, refer to [`azure_generic_resource`](azure_generic_resource.md#properties). | ||
|
||
Also, refer to [Azure documentation](https://docs.microsoft.com/en-us/rest/api/resources/subscriptions/get#subscription) for other properties available. | ||
Any attribute in the response may be accessed with the key names separated by dots (`.`), eg. `properties.<attribute>`. | ||
|
||
## Examples | ||
|
||
### Test Your Subscription`s Display Name | ||
```ruby | ||
describe azure_subscription do | ||
its('name') { should cmp 'Demo Resources' } | ||
end | ||
``` | ||
### Test Your Subscription`s Authorization Source | ||
```ruby | ||
describe azure_subscription do | ||
its('authorizationSource') { should cmp 'RoleBased' } | ||
end | ||
``` | ||
### Test Your Subscription`s Locations | ||
```ruby | ||
describe azure_subscription do | ||
its('locations') { should include('eastus') } | ||
end | ||
``` | ||
## Matchers | ||
|
||
This InSpec audit resource has the following special matchers. For a full list of available matchers, please visit our [Universal Matchers page](https://www.inspec.io/docs/reference/matchers/). | ||
|
||
### exists | ||
```ruby | ||
# If we expect a resource to always exist | ||
describe azure_subscription do | ||
it { should exist } | ||
end | ||
# If we expect a resource to never exist | ||
describe azure_subscription(id: 'fake_id') do | ||
it { should_not exist } | ||
end | ||
``` | ||
## Azure Permissions | ||
|
||
Your [Service Principal](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal) must be setup with a `contributor` role on the subscription you wish to test. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
--- | ||
title: About the azure_subscriptions Resource | ||
platform: azure | ||
--- | ||
|
||
# azure_subscriptions | ||
|
||
Use the `azure_subscriptions` InSpec audit resource to test properties and configuration of all Azure subscriptions for a tenant. | ||
|
||
## Azure REST API version, endpoint and http client parameters | ||
|
||
This resource interacts with api versions supported by the resource provider. | ||
The `api_version` can be defined as a resource parameter. | ||
If not provided, the latest version will be used. | ||
For more information, refer to [`azure_generic_resource`](azure_generic_resource.md). | ||
|
||
Unless defined, `azure_cloud` global endpoint, and default values for the http client will be used. | ||
For more information, refer to the resource pack [README](../../README.md). | ||
|
||
## Availability | ||
|
||
### Installation | ||
|
||
This resource is available in the [InSpec Azure resource pack](https://github.com/inspec/inspec-azure). | ||
For an example `inspec.yml` file and how to set up your Azure credentials, refer to resource pack [README](../../README.md#Service-Principal). | ||
|
||
## Syntax | ||
|
||
An `azure_subscriptions` resource block returns all subscription for a tenant. | ||
```ruby | ||
describe azure_subscriptions do | ||
it { should exist } | ||
end | ||
``` | ||
## Parameters | ||
|
||
- This resource does not require any parameters. | ||
|
||
## Properties | ||
|
||
|Property | Description | Filter Criteria<superscript>*</superscript> | | ||
|---------------|-------------------------------------------------------------|-----------------| | ||
| ids | A list of the subscription ids. | `id` | | ||
| names | A list of display names of all the subscriptions. | `name` | | ||
| tags | A list of `tag:value` pairs defined on the subscriptions. | `tags` | | ||
| tenant_ids | A list of tenant ids of all the subscriptions. | `tenant_id` | | ||
|
||
<superscript>*</superscript> For information on how to use filter criteria on plural resources refer to [FilterTable usage](https://github.com/inspec/inspec/blob/master/dev-docs/filtertable-usage.md). | ||
|
||
## Examples | ||
|
||
### Check a Specific Subscription is Present | ||
```ruby | ||
describe azure_subscriptions do | ||
its('names') { should include 'my-subscription' } | ||
end | ||
``` | ||
## Matchers | ||
|
||
This InSpec audit resource has the following special matchers. For a full list of available matchers, please visit our [Universal Matchers page](https://www.inspec.io/docs/reference/matchers/). | ||
|
||
### exists | ||
|
||
The control will pass if the filter returns at least one result. Use `should_not` if you expect zero matches. | ||
```ruby | ||
describe azure_subscriptions do | ||
it { should exist } | ||
end | ||
``` | ||
## Azure Permissions | ||
|
||
Your [Service Principal](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-create-service-principal-portal) must be setup with a `contributor` role on the subscription you wish to test. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
require 'azure_generic_resource' | ||
|
||
class AzureSubscription < AzureGenericResource | ||
name 'azure_subscription' | ||
desc 'Verifies settings for the current Azure Subscription' | ||
example <<-EXAMPLE | ||
describe azure_subscription do | ||
its('name') { should eq 'subscription-name' } | ||
its('locations') { should include 'eastus' } | ||
end | ||
EXAMPLE | ||
|
||
attr_reader :locations | ||
|
||
def initialize(opts = {}) | ||
# Options should be Hash type. Otherwise Ruby will raise an error when we try to access the keys. | ||
raise ArgumentError, 'Parameters must be provided in an Hash object.' unless opts.is_a?(Hash) | ||
raise ArgumentError, 'The `name` parameter is not allowed, use `id`, instead, in the format:'\ | ||
'`1e0b427a-aaaa-bbbb-1111-ee558463ebbf`' if opts.key?(:name) | ||
|
||
opts[:resource_provider] = specific_resource_constraint('subscriptions', opts) | ||
# This is an edge case resource where `id` becomes `name` from the backend perspective. | ||
# Environment variable will be used unless `id` is provided. | ||
opts[:name] = opts[:id] || ENV['AZURE_SUBSCRIPTION_ID'] | ||
opts[:resource_uri] = '/subscriptions/' | ||
opts[:add_subscription_id] = false | ||
opts[:allowed_parameters] = %i(locations_api_version id) | ||
|
||
# static_resource parameter must be true for setting the resource_provider in the backend. | ||
super(opts, true) | ||
|
||
return if failed_resource? | ||
@locations = collect_locations | ||
# This is for backward compatibility. | ||
define_singleton_method(:id) { subscriptionId } | ||
end | ||
|
||
def to_s | ||
super(AzureSubscription) | ||
end | ||
|
||
def name | ||
return unless exists? | ||
displayName | ||
end | ||
|
||
private | ||
|
||
def collect_locations | ||
return unless exists? | ||
api_version = @opts[:locations_api_version] || 'latest' | ||
api_response = get_resource({ resource_uri: id + '/locations', api_version: api_version }) | ||
api_response[:value].map { |location| location[:name] } | ||
end | ||
end | ||
|
||
# Provide the same functionality under the old resource name. | ||
# This is for backward compatibility. | ||
class AzurermSubscription < AzureSubscription | ||
name 'azurerm_subscription' | ||
desc 'Verifies settings for the current Azure Subscription' | ||
example <<-EXAMPLE | ||
describe azurerm_subscription do | ||
its('name') { should eq 'subscription-name' } | ||
its('locations') { should include 'eastus' } | ||
end | ||
EXAMPLE | ||
|
||
def initialize(opts = {}) | ||
opts[:locations_api_version] ||= '2019-10-01' | ||
Inspec::Log.warn Helpers.resource_deprecation_message(@__resource_name__, AzureSubscription.name) | ||
super | ||
end | ||
end |
Oops, something went wrong.