-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
193 additions
and
17 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,27 @@ | ||
// Copyright 2023 Google LLC. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package apikeys | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func Cmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "api-keys", | ||
Short: "API key management", | ||
} | ||
return cmd | ||
} |
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,15 @@ | ||
// Copyright 2023 Google LLC. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package apikeys |
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,15 @@ | ||
// Copyright 2023 Google LLC. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package apikeys |
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,15 @@ | ||
// Copyright 2023 Google LLC. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package apikeys |
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,15 @@ | ||
// Copyright 2023 Google LLC. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package apikeys |
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 |
---|---|---|
@@ -1,11 +1,95 @@ | ||
# petstore | ||
|
||
This directory contains subcommands that provide a demonstration API server | ||
that uses Service Control to check and log API requests. | ||
|
||
## Create credentials. | ||
|
||
To run the example, we'll need to call several Google APIs, and we'll | ||
authenticate our calls using a service account. Create a service account in the | ||
Cloud Console and download a JSON key. Move this file to | ||
`~/.config/zero/control.json`. | ||
|
||
The service account needs the following roles: | ||
|
||
- Service Management Administrator, to register and configure our service | ||
- Service Controller, to make calls to the Service Control API | ||
- API Keys Admin, to create and manage API keys | ||
|
||
## Register the service. | ||
|
||
Next we will use the Service Management API to create a "managed service". This | ||
is a top-level entity that we'll use to track service descriptions, access | ||
controls, and logs. | ||
|
||
The Service Management API requires that service names be domains or subdomains | ||
that are known to belong to us. That is because Service Infrastructure is often | ||
used to manage services that Google hosts. If we're only using Service Control | ||
that's not the case: Google is not hosting our service, but the requirement is | ||
still enforced. | ||
|
||
To use a domain that we own as our service name, we can prove our ownership by | ||
following the instructions | ||
[here](https://cloud.google.com/endpoints/docs/openapi/verify-domain-name). | ||
Once we've verified ownership, we can use our verified domain name or any | ||
subdomain as a service name. | ||
|
||
A simpler alternative is to instead use a domain name that Google controls and | ||
delegates to us. As described | ||
[here](https://cloud.google.com/endpoints/docs/openapi/configure-endpoints#use_a_domain_managed_by_google), | ||
these can be `*projectname*.appspot.com` or any subdomain of | ||
`endpoints.*projectname*.cloud.goog`. Subdomains of `*projectname*.appspot.com` | ||
are also ok, so if we had a project named `nerdvana`, we might create a service | ||
named `petstore.nerdvana.appspot.com`. | ||
|
||
Again, when we're only using Service Control, this name is just an identifier | ||
and doesn't need to match the hostname of our service. | ||
|
||
We can create the service using our CLI. | ||
|
||
``` | ||
zero service-management services create petstore.nerdvana.appspot.com --project nerdvana | ||
``` | ||
|
||
## Generate the Service Configuration. | ||
|
||
We describe our service to Service Infrastructure using a structure called | ||
Service Config. Service Config can be generated by tools that read API | ||
descriptions from various formats, but for our example, we will generate the | ||
Service Config directly with code in our CLI. | ||
|
||
We can run the following to view the Service Config: | ||
|
||
``` | ||
zero petstore config petstore.nerdvana.appspot.com | ||
``` | ||
|
||
We can apply it by adding the `--apply` and `--project` flags. | ||
|
||
``` | ||
zero petstore config petstore.nerdvana.appspot.com --project nerdvana | ||
zero service-management services rollouts create petstore.nerdvana.appspot.com 2023-10-07r0 | ||
``` | ||
|
||
Note the contents of the `id` field in the result. We'll need that to roll out | ||
our configuration. | ||
|
||
## Roll out the service configuration. | ||
|
||
``` | ||
zero service-management services rollouts create petstore.nerdvana.appspot.com 2023-10-12r0 | ||
``` | ||
|
||
Note that this is a long-running operation. We can check its status in the | ||
Cloud Console. | ||
|
||
## Generate an API key. | ||
|
||
## Run the server and make requests. | ||
|
||
``` | ||
ab -n 50 -c 10 -H "X-Api-Key: $APIKEY" http://localhost:8080/v1/pets | ||
ab -n 50 -c 10 -H "X-Api-Key: $APIKEY" http://localhost:8080/v1/pets/1 | ||
ab -n 50 -c 10 -H "X-Api-Key: $APIKEY" -p pet.json -T application/json http://localhost:8080/v1/pets | ||
``` | ||
|
||
## View the results in the Endpoints console. |