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

Site Shield Map Data Source added #248

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions docs/data-sources/siteshield_map.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
layout: "akamai"
page_title: "Akamai: Site Shield"
subcategory: "Site Shield Maps"
description: |-
Site Shield
---

# akamai_akamai_siteshield_map

Use the `akamai_akamai_siteshield_map` data source to retrieve information about the Site Shield maps, filtered by map ID. The information available is described
[here](https://developer.akamai.com/api/cloud_security/site_shield/v1.html#getamap).

## Example Usage

Basic usage:

```hcl
provider "akamai" {
edgerc = "~/.edgerc"
}

data "akamai_siteshield_map" "siteshield" {
map_id = 1234
}

output "siteshield_current_cidrs" {
value = data.akamai_siteshield_map.siteshield.current_cidrs
}

output "siteshield_proposed_cidrs" {
value = data.akamai_siteshield_map.siteshield.proposed_cidrs
}

output "siteshield_rule_name" {
value = data.akamai_siteshield_map.siteshield.rule_name
}

output "siteshield_acknowledged" {
value = data.akamai_siteshield_map.siteshield.acknowledged
}

```

## Argument Reference

* `map_id` - (Required) The map ID of a specific Site Shield map to retrieve.

The following arguments are supported:

## Attributes Reference

In addition to the arguments above, the following attributes are exported:

* `current_cidrs` - A list of current CIDRs configured for the specified SS map.

* `proposed_cidrs` - A list of proposed (new) CIDRs configured for the specified SS map.

* `rule_name` - A map rule name available shown in properties.

* `acknowledged` - A boolean of the aknowledgement state of the map.

2 changes: 1 addition & 1 deletion docs/guides/akamai_provider_auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ supporting API service names:
| **Identity and Access Management** | Identity Management: User Administration |
| **Network Lists** | Network Lists |
| **Property Provisioning** (Includes Common functions) | Property Manager (PAPI) |

| **Site Shield Maps** | Site Shield Maps |

-> **Note:** If you're using the Edge DNS or GTM module, you may also need the Property Manager API service. Whether you need this additional service depends on your contract and group. See [PAPI concepts](https://developer.akamai.com/api/core_features/property_manager/v1.html#papiconcepts) for more information.

Expand Down
2 changes: 2 additions & 0 deletions docs/guides/get_started_provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ At this point in the setup, you should refer to the guides for the Akamai module
| **Identity and Access Management** | [Identity and Access Management Module Guide](https://registry.terraform.io/providers/akamai/akamai/latest/docs/guides/get_started_iam) |
| **Network Lists** | [Network Lists Module Guide](https://registry.terraform.io/providers/akamai/akamai/latest/docs/guides/get_started_networklists) |
| **Property Provisioning** | [Property Provisioning Module Guide](https://registry.terraform.io/providers/akamai/akamai/latest/docs/guides/get_started_property) |
| **Site Shield Maps** | [Site Shield Maps Module Guide](https://registry.terraform.io/providers/akamai/akamai/latest/docs/guides/get_started_siteshield) |


-> **Note** Both Terraform and the Akamai Terraform CLI package come
pre-installed in the Akamai Development Environment. See [Set Up a Development Environment](https://developer.akamai.com/blog/2020/05/26/set-development-environment) for more information.
Expand Down
71 changes: 71 additions & 0 deletions docs/guides/get_started_siteshield.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---
layout: "akamai"
page_title: "Module: Site Shield Maps"
description: |-
Site Shield Maps module for the Akamai Terraform Provider
---

# Site Shield Maps Module Guide

The Akamai Site Shield Maps provider for Terraform gives you the ability to automate the retrieval of Site Shield maps used in various Akamai products. For customers who are already using the Akamai Network, Site Shield provides an additional layer of protection that helps prevent attackers from bypassing cloud-based protections to target the application origin. Site Shield cloaks websites and applications from the public Internet and restricts clients from directly accessing the origin. It is designed to complement the existing network infrastructure as well as advanced cloud security technologies available on the globally-distributed Akamai Intelligent Platform to mitigate the risks associated with network and application-layer threats that directly target the origin infrastructure. For more information about Site Shield Maps, see the [API documentation](https://developer.akamai.com/api/cloud_security/site_shield/v1.html)

## Configure the Terraform Provider

Set up your .edgerc credential files as described in [Get Started with Akamai APIs](https://developer.akamai.com/api/getting-started), and include read-write permissions for the Network Lists API.

1. Create a new folder called `terraform`
1. Inside the new folder, create a new file called `akamai.tf`.
1. Add the provider configuration to your `akamai.tf` file:

```hcl
provider "akamai" {
edgerc = "~/.edgerc"
config_section = "siteshield"
}
```

## Prerequisites

Review [Get Started with APIs](https://learn.akamai.com/en-us/learn_akamai/getting_started_with_akamai_developers/developer_tools/getstartedapis.html) for details on how to set up client tokens to access any Akamai API. These tokens appear as custom hostnames that look like this: https://akzz-XXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX.luna.akamaiapis.net.

To enable this API, choose the API service named Network Lists, and set the access level to READ-WRITE.

## Retrieving Site Shield Map Information

You can obtain a list of all network lists available for an authenticated user belonging to a group using the [`akamai_siteshield_map`](../data-sources/siteshield_map.md) data source and its `output_text` attribute. Add the following to your `akamai.tf` file:

```hcl
data "akamai_siteshield_map" "siteshield" {
map_id = 1234
}

output "siteshield_proposed_cidrs" {
value = data.akamai_siteshield_map.siteshield.proposed_cidrs
}
```

Once you have saved the file, switch to the terminal and initialize Terraform using the command:

```bash
$ terraform init
```

This command will install the latest version of the Akamai provider, as well as any other providers necessary. To update the Akamai provider version after a new release, simply run `terraform init` again.

## Test Your Configuration

To test your configuration, use `terraform plan`:

```bash
$ terraform plan
```

This command will make Terraform create a plan for the work it will do based on the configuration file. This will not actually make any changes and is safe to run as many times as you like.

## Apply Changes

To actually display the configuration information, or to create or modify resources as described further in this guide, we need to instruct Terraform to `apply` the changes outlined in the plan. To do this, in the terminal, run the command:

```bash
$ terraform apply
```
1 change: 1 addition & 0 deletions pkg/providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ import (
_ "github.com/akamai/terraform-provider-akamai/v2/pkg/providers/iam"
_ "github.com/akamai/terraform-provider-akamai/v2/pkg/providers/networklists"
_ "github.com/akamai/terraform-provider-akamai/v2/pkg/providers/property"
_ "github.com/akamai/terraform-provider-akamai/v2/pkg/providers/siteshield"
)
87 changes: 87 additions & 0 deletions pkg/providers/siteshield/data_akamai_siteshield_map.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package siteshield

import (
"context"
"errors"
"fmt"
"strconv"

"github.com/akamai/AkamaiOPEN-edgegrid-golang/v2/pkg/siteshield"
"github.com/akamai/terraform-provider-akamai/v2/pkg/akamai"
"github.com/akamai/terraform-provider-akamai/v2/pkg/tools"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceSiteShieldMap() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceSiteShieldMapRead,
Schema: map[string]*schema.Schema{
"map_id": {
Type: schema.TypeInt,
Required: true,
},
"current_cidrs": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"proposed_cidrs": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"rule_name": {
Type: schema.TypeString,
Computed: true,
},
"acknowledged": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}

func dataSourceSiteShieldMapRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
meta := akamai.Meta(m)
client := inst.Client(meta)
logger := meta.Log("SSMAP", "dataSiteShieldMap")

mapID, err := tools.GetIntValue("map_id", d)
d.SetId(strconv.Itoa(mapID))

if err != nil && !errors.Is(err, tools.ErrNotFound) {
return diag.FromErr(err)
}

ssMapID := siteshield.SiteShieldMapRequest{UniqueID: mapID}

ssMap, err := client.GetSiteShieldMap(ctx, ssMapID)
if err != nil {
logger.Errorf("calling 'getSiteShieldMap': %s", err.Error())
return diag.FromErr(err)
}

if err := d.Set("current_cidrs", ssMap.CurrentCidrs); err != nil {
logger.Errorf("error setting 'current_cidrs': %s", err.Error())
return diag.FromErr(fmt.Errorf("%w: %s", tools.ErrValueSet, err.Error()))
}

if err := d.Set("proposed_cidrs", ssMap.ProposedCidrs); err != nil {
logger.Errorf("error setting 'proposed_cidrs': %s", err.Error())
return diag.FromErr(fmt.Errorf("%w: %s", tools.ErrValueSet, err.Error()))
}

if err := d.Set("rule_name", ssMap.RuleName); err != nil {
logger.Errorf("error setting 'rule_name': %s", err.Error())
return diag.FromErr(fmt.Errorf("%w: %s", tools.ErrValueSet, err.Error()))
}

if err := d.Set("acknowledged", ssMap.Acknowledged); err != nil {
logger.Errorf("error setting 'acknowledged': %s", err.Error())
return diag.FromErr(fmt.Errorf("%w: %s", tools.ErrValueSet, err.Error()))
}

return nil
}
45 changes: 45 additions & 0 deletions pkg/providers/siteshield/data_akamai_siteshield_map_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package siteshield

import (
"encoding/json"
"testing"

"github.com/akamai/AkamaiOPEN-edgegrid-golang/v2/pkg/siteshield"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/stretchr/testify/mock"
)

func TestAccAkamaiSiteShield_data_basic(t *testing.T) {
t.Run("get SiteShield map", func(t *testing.T) {
client := &mocksiteshield{}

cv := siteshield.SiteShieldMapResponse{}
expectJS := compactJSON(loadFixtureBytes("testdata/TestDSSiteShield/SiteShield.json"))
json.Unmarshal([]byte(expectJS), &cv)

client.On("GetSiteShieldMap",
mock.Anything,
siteshield.SiteShieldMapRequest{UniqueID: 1234},
).Return(&cv, nil)

useClient(client, func() {
resource.Test(t, resource.TestCase{
IsUnitTest: true,
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: loadFixtureString("testdata/TestDSSiteShield/get_map.tf"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.akamai_siteshield_map.test", "map_id", "1234"),
resource.TestCheckResourceAttr("data.akamai_siteshield_map.test", "rule_name", "a;s36.akamai.net"),
resource.TestCheckResourceAttr("data.akamai_siteshield_map.test", "acknowledged", "false"),
),
},
},
})
})

client.AssertExpectations(t)
})

}
Loading