Jump to: navigation, search

Difference between revisions of "NovaApiValidationFramework"

(Migration plan)
(Combination of v2.1 and v3 APIs)
 
(196 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
<big>'''Nova API Validation Framework'''</big>
 
<big>'''Nova API Validation Framework'''</big>
 +
 +
== Discussion ==
 +
https://etherpad.openstack.org/NovaApiValidationFramework
  
 
== Introduction ==
 
== Introduction ==
  
Nova is a HTTP service and it has RESTful APIs.<br />
+
Nova is a RESTful API based HTTP service.<br />
These APIs provide server capacity in the cloud.<br />
+
These APIs allow for cloud managed server operations.<br />
 
For example, we can create a virtual machine instance by sending the following POST request to Nova's URI: /servers.<br />
 
For example, we can create a virtual machine instance by sending the following POST request to Nova's URI: /servers.<br />
 
  {
 
  {
Line 13: Line 16:
 
     }
 
     }
 
  }
 
  }
Nova has many RESTful APIs, because of many features.<br />
+
Nova contains many RESTful APIs to facilitate its many features.<br />
Moreover, each RESTful API has multiple parameters to control each feature flexibly.<br />
+
Moreover, each RESTful API can be controlled flexibly through multiple parameters.<br />
For example, the above "create a virtual machine instance" API has 19 parameters which include "name", "imageRef", etc.<br />
+
For example, the above "create a virtual machine instance" API has 19 parameters including "name", "imageRef", etc.<br />
 
<br />
 
<br />
HTTP service should validate generally every API parameters in terms of acceptable types, minimum and maximum length and ranges.<br />
+
The HTTP service need to validate practically every API parameter in terms of acceptable type and minimum/maximum length and range.<br />
The service can avoid unnecessary workload by validating API parameters before API operation.<br />
+
Unnecessary workload can be avoided by validating API parameters before executing the API operation.<br />
Moreover, API validation is important feature from the viewpoint of HTTP service security.<br />
+
Moreover, API validation is an important feature from the viewpoint of HTTP service security.<br />
  
=== Background ===
 
  
Figure 1 shows current API validation overview in Nova.<br />
+
As Nova is an HTTP service, it should be able to validate API parameters as follows:
[[File:Current validation.png|none|Figure 1]]
+
* Validate every API parameter.
'''Figure 1'''
+
* Return an error response before API operation, if API parameter is invalid.
 +
* Unify the error message format of the response, if the cause is the same.<br />(ex) ".. is too short.", ".. is too long.", ".. is not integer."
  
Nova validates API parameters in a method corresponding to the received API request.<br />
+
== What is necessary for Nova-v3-API ==
There are validations and operations in each API method.<br />
 
It is possible to execute some operation even if some parameter is invalid, because the operation is executed before some parameter validation.<br />
 
Not all API parameters are completely validated, so many API operations are executed without API parameter validations.<br />
 
Furthermore, it is difficult to handle error message on the API caller side because error message formats are not unified.<br />
 
  
=== Objectives ===
+
This framework target is Nova-v3-API, because it is new and the API compatibility issues would not happen.<br />
 +
On Havana release, Nova-v3-API is experimental and the API will become generic in Icehouse.<br />
 +
In Icehouse development cycle, the web framework of Nova-v3-API would move to Pecan/WSME.<br />
 +
WSME has original API validation mechanism already, and we'd better to find good API validation feature if we need it.<br />
 +
To find good feature, we'd better to discuss it based on current Nova-v3-API implementation.<br />
  
Nova is one of HTTP services, and it should validate API parameters based on the following:
+
This investigation is based on the following commit of Nova source code:
* Validate every API parameters.
+
commit 5f0591252dc5d6e3f49682f85dcdbd99f692c07a
* Return an error response before API operation, if API parameter is invalid.
+
Merge: 0211752 a19d72b
* Unify an error message format of the response, if the cause is the same.<br />(ex) ".. is too short.", ".. is too long.", ".. is not integer."
+
Author: Jenkins <jenkins@review.openstack.org>
 +
Date:  Mon Oct 7 03:14:43 2013 +0000
 +
 +
    Merge "Fixes rescue doesn't honor enable password conf for v3"
  
== Proposal ==
+
=== Basic validation feature ===
For the comprehensive validation, I'd like to propose API validation framework.<br />
 
  
=== Validation point ===
+
The table of [[NovaApiValidationFramework#Appendix:_Nova-v3-API_parameters|Appendix: Nova-v3-API parameters]] shows all parameters of Nova-v3-API.<br />
 +
It seems that we need some basic validation features such as:
 +
* Type validation
 +
** str(name, ..), int(vcpus, ..), float(rxtx_factor), dict(metadata, ..), list(networks, ..), bool(conbine, ..), None(availability_zone)
 +
* String length validation
 +
** 1 - 255
 +
* Value range validation
 +
** value >= 0(rotation, ..), value > 0(vcpus, ..), value >= 1(os-multiple-create:min_count, os-multiple-create:max_count)
 +
* Data format validation
 +
** '''Pattern:''' uuid(volume_id, ..), boolean(on_shared_storage, ..), base64encoded(contents), ipv4(access_ip_v4,  fixed_ip), ipv6(access_ip_v6)
 +
** '''Allowed list:''' 'active' or 'error'(state), 'parent' or 'child'(cells.type), 'MANUAL' or 'AUTO'(os-disk-config:disk_config), ...
 +
** '''Allowed string:''' not contain '!' and '.'(cells.name), contain [a-zA-Z0-9_.- ] only(flavor.name, flavor.id)
 +
* Mandatory validation
 +
** '''Required:''' server.name, flavor.name, ..
 +
** '''Optional:''' flavor.ephemeral, flavor.swap, ..
  
This framework separates API validations from API methods, and executes API validations before each API method.<br />
+
=== Auxiliary validation feature ===
Figure 2 shows API validation of this framework.<br />
 
# Select both API schema definition and API method corresponding to the received API request.
 
# Validate API parameters by the API schema definition.
 
# Execute API method if the API validation is succeeded.
 
[[File:New validation.png|none|Figure 2]]
 
'''Figure 2'''
 
  
=== Merits ===
+
Some parameters have dependency between other parameter.<br />
 +
For example, name or/and availability_zone should be specified when updating an aggregate.<br />
 +
The parameter dependencies are few cases, and the dependency validation feature would not be mandatory.<br />
 +
The cases are the following:
 +
* '''Required if not specifying other:''' (update aggregate: name or availability_zone), (host: status or maintenance_mode), (server: os-block-device-mapping:block_device_mapping or image_ref)
 +
* '''Should not specify both:''' (interface_attachment: net_id and port_id), (server: fixed_ip and port)
  
The OpenStack community releases a new Nova every six months, and the Nova APIs are increasing version by version.<br />
+
== Implementation ==
Now Nova has hundreds of its API.<br />
 
We need a lot of cooperation for both the implementation of API schema definition and its review due to many APIs, but this framework is worth implementing because of the following merits.<br />
 
 
 
* Clarify the API parameter definitions.<br />The API schema definitions clarify each type and acceptable range and length.<br />By schema definitions of every APIs, developers will be able to create easily an application which uses the Nova APIs.
 
* Clean up codes<br />We can reduce Nova codes by merging validations and error response methods.<br />For example, there are the following 8 validation lines for a parameter "min_count" of "create a virtual machine instance" API.
 
        try:
 
            min_count = int(str(min_count))
 
        except ValueError:
 
            msg = _('min_count must be an integer value')
 
            raise exc.HTTPBadRequest(explanation=msg)
 
        if min_count < 1:
 
            msg = _('min_count must be > 0')
 
            raise exc.HTTPBadRequest(explanation=msg)
 
:By this framework, we will be able to merge these lines to the following 1 line.
 
        'min_count': {'type': 'integer', 'minimum': 1},
 
  
== Implementation ==
+
In Icehouse summit, we have gotten a consensus API validation of v3 API will be implemented with JSONSchema.
 +
The implementation is going on https://review.openstack.org/#/q/project:openstack/nova+branch:master+topic:bp/v3-api-schema,n,z
  
I investigated API validation framework by creating a prototype with JSON Schema library.<br />
+
== Combination of v2.1 and v3 APIs ==
The prototype validates "create a virtual machine instance" API, because the API is most popular<br />
 
and it has many API parameters. I will implement the other API validations after discussions at the OpenStack community.
 
  
=== API parameter type ===
+
[[File:Nova-v2-v3.png]]
  
The default types of JSON Schema are basic types such as integer, float, string, and boolean.<br />
+
== Appendix: Nova-v3-API parameters ==
Nova has many APIs, and we need additional types(uuid, url, etc.) for strict validation.<br />
 
Current expected types are the following:
 
* integer<br />minimum, maximum
 
* float<br />minimum, maximum
 
* string<br />minLength, maxLength
 
* uuid
 
* url
 
* ipv4 address
 
* ipv6 address
 
* base64 encoded data
 
<br />
 
On JSON Schema library, the type validation is implemented by isinstance() method.<br />
 
isinstance() calls the meta class's __instancecheck__() method, which is specified as argument the type class.<br />
 
Therefore, we can implement additional types by defining both meta class and type class like the following:
 
class ipv4Meta(type):
 
    def __instancecheck__(self, instance):
 
        return utils.is_valid_ipv4(instance)
 
 
class ipv4:
 
    __metaclass__ = ipv4Meta
 
  
We can validate API parameters with additional types by specifying the argument "types" of a Validator instance.
+
This investigation is based on the following commit of Nova source code:
  types={
+
  commit 5f0591252dc5d6e3f49682f85dcdbd99f692c07a
    'ipv4': ipv4,
+
Merge: 0211752 a19d72b
    'ipv6': ipv6,
+
Author: Jenkins <jenkins@review.openstack.org>
    [..]
+
  Date:  Mon Oct 7 03:14:43 2013 +0000
  }
 
 
   
 
   
validator = jsonschema.Draft3Validator(schema, types=types)
+
     Merge "Fixes rescue doesn't honor enable password conf for v3"
try:
 
     validator.validate(action_args['body'])
 
except jsonschema.ValidationError as ex:
 
    msg = _("Invalid API parameter: %s") % ex.args[0]
 
    return Fault(webob.exc.HTTPBadRequest(explanation=msg))
 
 
 
=== API schema ===
 
 
 
On the prototype, I defined API schema definitions as JSON Schema.<br />
 
Each API parameter is defined by additional types which are described in "3.1. API parameter type".<br />
 
The following shows the schema of "create a virtual machine instance" API.<br />
 
Moreover, necessary API parameters are defined with "'required': True".
 
{
 
    'type' : 'object',
 
    'properties' : {
 
        'server': {
 
            'properties' : {
 
                'name': {'type': 'string', 'minLength': 1,
 
                          'maxLength': 255, 'required': True},
 
                'imageRef': {'type': 'intOrUuidOrUrl', 'required': True},
 
                'flavorRef': {'type': 'intOrUuidOrUrl', 'required': True},
 
                'min_count': {'type': 'integer', 'minimum': 1},
 
                'max_count': {'type': 'integer', 'minimum': 1},
 
                'accessIPv4': {'type': 'ipv4'},
 
                'accessIPv6': {'type': 'ipv6'},
 
[..]
 
            }
 
        }
 
    }
 
}
 
On this prototype, API schema is defined near each API method in the same source file, because I feel it is easy to define each API schema.<br />
 
On the other hand, it may be better to define each schema in different file from the source code because we can know all API schema definitions in one file.<br />
 
It is better to reconsider this when API schema definitions increase.
 
 
 
=== Migration plan ===
 
TBD
 
 
 
== Future ==
 
  
I wish the following implementations after applying this framework to every Nova APIs.
+
The source files of Nova-v3-API are put under ./nova/api/openstack/compute/plugins/v3/, and there are 79 API methods.<br />
 +
In these APIs, 49 APIs use API parameters of a request body, and they have 148 API parameters.<br />
 +
(32% API parameters(48/148) are not validated with any ways.)<br/>
 +
The following table shows all API parameters and each API validating implementation.<br />
  
* Add the existence check of API parameter definition to Jenkins<br />OpenStack has a good development process by Jenkins which is used to continuously test to verify the coding style and to ensure functionality, when the code change is proposed. We feel it is good to add the existence check of API parameter definition to this process. By this process, we will be able to keep API security.
+
* <big>NV</big>: any elements are not validated
  
* Create API specification document<br />We think it is possible to create the elements of API specification document from the API schema definition.<br />OpenStack community has API specification document of all OpenStack components[1] and the one of each component[2].<br />We will be able to get certain elements of all the API parameters from the API schema definition.<br />[1]: http://api.openstack.org/api-ref.html<br />[2]: Nova: http://docs.openstack.org/api/openstack-compute/2/content/
+
{| class="wikitable"
 +
|-
 +
! Class !! API method !! API parameter !! Required/Optional !! Type !! Length/Range !! Data format
 +
|-
 +
| AdminActionsController || _create_backup() || name || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || backup_type || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || rotation || Required || int ||  >= 0 ||
 +
|-
 +
|  ||  || metadata || Optional || dict || "{key: value} 1 < len(key) < 255" ||
 +
|-
 +
| AdminActionsController || _migrate_live() || block_migration || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || disk_over_commit || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || host || Required || <big>NV</big> ||  ||
 +
|-
 +
| AdminActionsController || _reset_state() || state || Required || str ||  || "active"or "error"
 +
|-
 +
| AdminPasswordController || change_password() || admin_password || Required || str ||  ||
 +
|-
 +
| AgentController || create() || hypervisor || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || os || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || architecture || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || version || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || url || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || md5hash || Required || <big>NV</big> ||  ||
 +
|-
 +
| AgentController || update() || url || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || md5hash || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || version || Required || <big>NV</big> ||  ||
 +
|-
 +
| AggregateController || create() || name || Required || str || 1 - 255 ||
 +
|-
 +
|  ||  || availability_zone || Required || None or str ||  ||
 +
|-
 +
| AggregateController || update() || name || Required if not specifying availability_zone || str || 1 - 255 ||
 +
|-
 +
|  ||  || availability_zone || Required if not specifying name || None or str ||  ||
 +
|-
 +
| AggregateController || _add_host() || host || Required || str ||  ||
 +
|-
 +
| AggregateController || _remove_host() || host || Required || str ||  ||
 +
|-
 +
| AggregateController || _set_metadata() || metadata || Required || dict ||  ||
 +
|-
 +
| InterfaceAttachmentController || create() || net_id || Optional, Should not specify both net_id and port_id || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || port_id || Optional, Should not specify both net_id and port_id || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || ip_address || Optional, Should specify both req_ip and net_id || <big>NV</big> ||  ||
 +
|-
 +
| CellsController || create() || name || Required || str ||  || not "!" and "."
 +
|-
 +
|  ||  || type || Optional || str ||  || "parent" or "child"
 +
|-
 +
| CellsController || update() || name || Optional || str ||  || not "!" and "."
 +
|-
 +
|  ||  || type || Optional || str ||  || "parent" or "child"
 +
|-
 +
| CellsController || sync_instances() || project_id || Optional || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || deleted || Optional || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || updated_since || Optional || <big>NV</big> ||  ||
 +
|-
 +
| ConsoleOutputController || get_console_output() || length || Optional || int ||  || Cast by int(str(length))
 +
|-
 +
| CoverageController || _start_coverage() || combine || Optional || bool ||  || boolean
 +
|-
 +
| CoverageController || _report_coverage() || file || Optional || str ||  || filename (if path != os.path.basename(path): Error)
 +
|-
 +
|  ||  || xml || Optional || bool ||  ||
 +
|-
 +
|  ||  || html || Optional || bool ||  ||
 +
|-
 +
| ServerDiskConfigController || create() || os-disk-config:disk_config || Optional || str ||  || "MANUAL" or "AUTO"
 +
|-
 +
| ServerDiskConfigController || update() || os-disk-config:disk_config || Optional || str ||  || "MANUAL" or "AUTO"
 +
|-
 +
| ServerDiskConfigController || _action_rebuild() || os-disk-config:disk_config || Required || str ||  || "MANUAL" or "AUTO"
 +
|-
 +
| ServerDiskConfigController || _action_resize() || os-disk-config:disk_config || Required || str ||  || "MANUAL" or "AUTO"
 +
|-
 +
| EvacuateController || _evacuate() || host || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || on_shared_storage || Required || str ||  || boolean
 +
|-
 +
|  ||  || admin_password || Optional || <big>NV</big> ||  ||
 +
|-
 +
| ExtendedVolumesController || swap() || old_volume_id || Required || str ||  || uuid
 +
|-
 +
|  ||  || new_volume_id || Required || str ||  || uuid
 +
|-
 +
| ExtendedVolumesController || attach() || volume_id || Required || str ||  || uuid
 +
|-
 +
|  ||  || device || Required || <big>NV</big> ||  ||
 +
|-
 +
| ExtendedVolumesController || detach() || volume_id || Required || str ||  || uuid
 +
|-
 +
| FlavorActionController || _add_tenant_access() || tenant_id || Required || <big>NV</big> ||  ||
 +
|-
 +
| FlavorActionController || _remove_tenant_access() || tenant_id || Required || <big>NV</big> ||  ||
 +
|-
 +
| FlavorManageController || _create() || name || Required || str || 1 - 255 || names can only contain [a-zA-Z0-9_.- ]
 +
|-
 +
|  ||  || id || Required || int or str || 1 - 255 || id can only contain [a-zA-Z0-9_.-]
 +
|-
 +
|  ||  || ram || Required || int ||  > 0 ||
 +
|-
 +
|  ||  || vcpus || Required || int ||  > 0 ||
 +
|-
 +
|  ||  || disk || Required || int ||  >= 0 ||
 +
|-
 +
|  ||  || ephemeral || Optional || int ||  >= 0 ||
 +
|-
 +
|  ||  || swap || Optional || int ||  >= 0 ||
 +
|-
 +
|  ||  || rxtx_factor || Optional || float ||  > 0 ||
 +
|-
 +
|  ||  || os-flavor-access:is_public || Optional || str ||  || '1', 't', 'true', 'on', 'y', 'yes', '0', 'f', 'false', 'off', 'n', 'no'
 +
|-
 +
| FlavorExtraSpecsController || create() || extra_specs || Optional || dict ||  ||
 +
|-
 +
| FlavorExtraSpecsController || update() ||  || Optional || dict ||  ||
 +
|-
 +
| HostController || update() || status || Required if not specifying maintenance_mode || str ||  || "enable" or "disable"
 +
|-
 +
|  ||  || maintenance_mode || Required if not specifying status || str ||  || "enable" or "disable"
 +
|-
 +
| KeypairController || create() || name || Required || str? (not type checked) || 1 - 255 ||
 +
|-
 +
|  ||  || public_key || Optional || <big>NV</big> ||  ||
 +
|-
 +
| MultinicController || _add_fixed_ip() || network_id || Required || <big>NV</big> ||  ||
 +
|-
 +
| MultinicController || _remove_fixed_ip() || address || Required || <big>NV</big> ||  ||
 +
|-
 +
| QuotaClassSetsController || update() || instances || Optional || int ||  ||
 +
|-
 +
|  ||  || cores || Optional || int ||  ||
 +
|-
 +
|  ||  || ram || Optional || int ||  ||
 +
|-
 +
|  ||  || security_groups || Optional || int ||  ||
 +
|-
 +
|  ||  || floating_ips || Optional || int ||  ||
 +
|-
 +
|  ||  || fixed_ips || Optional || int ||  ||
 +
|-
 +
|  ||  || metadata_items || Optional || int ||  ||
 +
|-
 +
|  ||  || injected_files || Optional || int ||  ||
 +
|-
 +
|  ||  || injected_file_content_bytes || Optional || int ||  ||
 +
|-
 +
|  ||  || injected_file_path_bytes || Optional || int ||  ||
 +
|-
 +
|  ||  || security_group_rules || Optional || int ||  ||
 +
|-
 +
|  ||  || key_pairs || Optional || int ||  ||
 +
|-
 +
| QuotaSetsController || update() || instances || Optional || int ||  ||
 +
|-
 +
|  ||  || cores || Optional || int ||  ||
 +
|-
 +
|  ||  || ram || Optional || int ||  ||
 +
|-
 +
|  ||  || security_groups || Optional || int ||  ||
 +
|-
 +
|  ||  || floating_ips || Optional || int ||  ||
 +
|-
 +
|  ||  || fixed_ips || Optional || int ||  ||
 +
|-
 +
|  ||  || metadata_items || Optional || int ||  ||
 +
|-
 +
|  ||  || injected_files || Optional || int ||  ||
 +
|-
 +
|  ||  || injected_file_content_bytes || Optional || int ||  ||
 +
|-
 +
|  ||  || injected_file_path_bytes || Optional || int ||  ||
 +
|-
 +
|  ||  || security_group_rules || Optional || int ||  ||
 +
|-
 +
|  ||  || key_pairs || Optional || int ||  ||
 +
|-
 +
|  ||  || force || Optional || str ||  || boolean
 +
|-
 +
| RescueController || _rescue() || admin_pass || Optional || <big>NV</big> ||  ||
 +
|-
 +
| SchedulerHintsController || create() || os-scheduler-hints:scheduler_hints || Optional || <big>NV</big> ||  ||
 +
|-
 +
| ServerMetadataController || create() || metadata || Required || dict ||  ||
 +
|-
 +
| ServerMetadataController || update() || metadata || Required || dict || len(dict) == 1 ||
 +
|-
 +
| ServerMetadataController || update_all() || metadata || Required || dict ||  ||
 +
|-
 +
| ServersController || create() || admin_pass || Optional || str ||  ||
 +
|-
 +
|  ||  || name || Required || str || 1 - 255 ||
 +
|-
 +
|  ||  || image_ref || Required if not specifying os-block-device-mapping:block_device_mapping || str ||  || uuid or ".*/<uuid>"
 +
|-
 +
|  ||  || os-block-device-mapping:block_device_mapping || Required if not specifying image_ref || dict ||  ||
 +
|-
 +
|  ||  ||  source_type || Required || str ||  || 'volume', 'image', 'snapshot', 'blank'
 +
|-
 +
|  ||  ||  uuid || Required if source_type is not 'blank' || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || os-availability-zone:availability_zone || Optional || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || os-config-drive:config_drive || Optional || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || auto_disk_config || Optional || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || key_name || Optional || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || os-multiple-create:min_count || Optional || int ||  >= 1 ||
 +
|-
 +
|  ||  || os-multiple-create:max_count || Optional || int ||  >= 1 ||
 +
|-
 +
|  ||  || os-multiple-create:return_reservation_id || Optional || bool ||  ||
 +
|-
 +
|  ||  || personality || Optional || dict ||  ||
 +
|-
 +
|  ||  ||  path || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  ||  contents || Required || str ||  || base64encoded
 +
|-
 +
|  ||  || scheduler_hints || Optional || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || os-security-groups:security_groups || Optional || list ||  ||
 +
|-
 +
|  ||  ||  name || Optional || str ||  ||
 +
|-
 +
|  ||  || os-user-data:user_data || Optional || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || networks || Optional || list of dict ||  ||
 +
|-
 +
|  ||  ||  fixed_ip || Optional, Should not specify both fixed_ip and port ||  ||  || ipv4
 +
|-
 +
|  ||  ||  port || Optional, Should not specify both fixed_ip and port ||  ||  || uuid
 +
|-
 +
|  ||  || access_ip_v4 || Optional ||  ||  || ipv4
 +
|-
 +
|  ||  || access_ip_v6 || Optional ||  ||  || ipv6
 +
|-
 +
|  ||  || flavor_ref || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || metadata || Optional ||  || {key: value}, 1 < len(key) < 255, len(value) < 255" ||
 +
|-
 +
| ServersController || update() || name || Optional || str || 1 - 255 ||
 +
|-
 +
|  ||  || access_ip_v4 || Optional ||  ||  || ipv4
 +
|-
 +
|  ||  || access_ip_v6 || Optional ||  ||  || ipv6
 +
|-
 +
|  ||  || auto_disk_config || Optional || <big>NV</big> ||  ||
 +
|-
 +
| ServersController || _action_reboot() || type || Required || str ||  || "HARD" or "SOFT", not allowed lower case.
 +
|-
 +
| ServersController || _action_resize() || flavor_ref || Required || <big>NV</big> ||  || validated with the existing flavors.
 +
|-
 +
| ServersController || _action_rebuild() || image_ref || Required || str ||  || uuid or ".*/<uuid>"
 +
|-
 +
|  ||  || admin_pass || Optional || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || name || Optional || str || 1 - 255 ||
 +
|-
 +
|  ||  || access_ip_v4 || Optional ||  ||  || ipv4
 +
|-
 +
|  ||  || access_ip_v6 || Optional ||  ||  || ipv6
 +
|-
 +
|  ||  || auto_disk_config || Optional || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || personality || Optional || <big>NV</big> ||  ||
 +
|-
 +
|  ||  ||  path || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  ||  contents || Required || str ||  || base64encoded
 +
|-
 +
| ServersController || _action_create_image() || name || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || metadata || Optional || dict ||  ||
 +
|-
 +
| ServiceController || update() || host || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || binary || Required || <big>NV</big> ||  ||
 +
|-
 +
|  ||  || disabled_reason || Required if id is disable-log-reason || str || 1 - 255 ||
 +
|}
  
* Apply API validation framework to other components<br />By applying this framework to other components, we can make whole OpenStack secure.<br />If we can succeed to apply this framework to Nova which has many APIs and complex API parameters, we will be able to apply it to other components.<br />I expect cooperation of many developers.
+
== Appendix: URL ==
 +
* Discussion
 +
** https://etherpad.openstack.org/NovaApiValidationFramework
 +
** http://lists.openstack.org/pipermail/openstack-dev/2013-October/016649.html
 +
* API validation coverage improvement
 +
** https://etherpad.openstack.org/p/NovaApiValidationCoverage

Latest revision as of 08:40, 24 April 2014

Nova API Validation Framework

Discussion

https://etherpad.openstack.org/NovaApiValidationFramework

Introduction

Nova is a RESTful API based HTTP service.
These APIs allow for cloud managed server operations.
For example, we can create a virtual machine instance by sending the following POST request to Nova's URI: /servers.

{
   "server" : {
       "name" : "new-server-test",
       "imageRef" : "http://servers.api.openstack.org/1234/images/52415800-8b69-11e0-9b19-734f6f006e54",
       "flavorRef" : 1
   }
}

Nova contains many RESTful APIs to facilitate its many features.
Moreover, each RESTful API can be controlled flexibly through multiple parameters.
For example, the above "create a virtual machine instance" API has 19 parameters including "name", "imageRef", etc.

The HTTP service need to validate practically every API parameter in terms of acceptable type and minimum/maximum length and range.
Unnecessary workload can be avoided by validating API parameters before executing the API operation.
Moreover, API validation is an important feature from the viewpoint of HTTP service security.


As Nova is an HTTP service, it should be able to validate API parameters as follows:

  • Validate every API parameter.
  • Return an error response before API operation, if API parameter is invalid.
  • Unify the error message format of the response, if the cause is the same.
    (ex) ".. is too short.", ".. is too long.", ".. is not integer."

What is necessary for Nova-v3-API

This framework target is Nova-v3-API, because it is new and the API compatibility issues would not happen.
On Havana release, Nova-v3-API is experimental and the API will become generic in Icehouse.
In Icehouse development cycle, the web framework of Nova-v3-API would move to Pecan/WSME.
WSME has original API validation mechanism already, and we'd better to find good API validation feature if we need it.
To find good feature, we'd better to discuss it based on current Nova-v3-API implementation.

This investigation is based on the following commit of Nova source code:

commit 5f0591252dc5d6e3f49682f85dcdbd99f692c07a	
Merge: 0211752 a19d72b	
Author: Jenkins <jenkins@review.openstack.org>	
Date:   Mon Oct 7 03:14:43 2013 +0000	

    Merge "Fixes rescue doesn't honor enable password conf for v3"

Basic validation feature

The table of Appendix: Nova-v3-API parameters shows all parameters of Nova-v3-API.
It seems that we need some basic validation features such as:

  • Type validation
    • str(name, ..), int(vcpus, ..), float(rxtx_factor), dict(metadata, ..), list(networks, ..), bool(conbine, ..), None(availability_zone)
  • String length validation
    • 1 - 255
  • Value range validation
    • value >= 0(rotation, ..), value > 0(vcpus, ..), value >= 1(os-multiple-create:min_count, os-multiple-create:max_count)
  • Data format validation
    • Pattern: uuid(volume_id, ..), boolean(on_shared_storage, ..), base64encoded(contents), ipv4(access_ip_v4, fixed_ip), ipv6(access_ip_v6)
    • Allowed list: 'active' or 'error'(state), 'parent' or 'child'(cells.type), 'MANUAL' or 'AUTO'(os-disk-config:disk_config), ...
    • Allowed string: not contain '!' and '.'(cells.name), contain [a-zA-Z0-9_.- ] only(flavor.name, flavor.id)
  • Mandatory validation
    • Required: server.name, flavor.name, ..
    • Optional: flavor.ephemeral, flavor.swap, ..

Auxiliary validation feature

Some parameters have dependency between other parameter.
For example, name or/and availability_zone should be specified when updating an aggregate.
The parameter dependencies are few cases, and the dependency validation feature would not be mandatory.
The cases are the following:

  • Required if not specifying other: (update aggregate: name or availability_zone), (host: status or maintenance_mode), (server: os-block-device-mapping:block_device_mapping or image_ref)
  • Should not specify both: (interface_attachment: net_id and port_id), (server: fixed_ip and port)

Implementation

In Icehouse summit, we have gotten a consensus API validation of v3 API will be implemented with JSONSchema. The implementation is going on https://review.openstack.org/#/q/project:openstack/nova+branch:master+topic:bp/v3-api-schema,n,z

Combination of v2.1 and v3 APIs

Nova-v2-v3.png

Appendix: Nova-v3-API parameters

This investigation is based on the following commit of Nova source code:

commit 5f0591252dc5d6e3f49682f85dcdbd99f692c07a	
Merge: 0211752 a19d72b	
Author: Jenkins <jenkins@review.openstack.org>	
Date:   Mon Oct 7 03:14:43 2013 +0000	

    Merge "Fixes rescue doesn't honor enable password conf for v3"	

The source files of Nova-v3-API are put under ./nova/api/openstack/compute/plugins/v3/, and there are 79 API methods.
In these APIs, 49 APIs use API parameters of a request body, and they have 148 API parameters.
(32% API parameters(48/148) are not validated with any ways.)
The following table shows all API parameters and each API validating implementation.

  • NV: any elements are not validated
Class API method API parameter Required/Optional Type Length/Range Data format
AdminActionsController _create_backup() name Required NV
backup_type Required NV
rotation Required int >= 0
metadata Optional dict "{key: value} 1 < len(key) < 255"
AdminActionsController _migrate_live() block_migration Required NV
disk_over_commit Required NV
host Required NV
AdminActionsController _reset_state() state Required str "active"or "error"
AdminPasswordController change_password() admin_password Required str
AgentController create() hypervisor Required NV
os Required NV
architecture Required NV
version Required NV
url Required NV
md5hash Required NV
AgentController update() url Required NV
md5hash Required NV
version Required NV
AggregateController create() name Required str 1 - 255
availability_zone Required None or str
AggregateController update() name Required if not specifying availability_zone str 1 - 255
availability_zone Required if not specifying name None or str
AggregateController _add_host() host Required str
AggregateController _remove_host() host Required str
AggregateController _set_metadata() metadata Required dict
InterfaceAttachmentController create() net_id Optional, Should not specify both net_id and port_id NV
port_id Optional, Should not specify both net_id and port_id NV
ip_address Optional, Should specify both req_ip and net_id NV
CellsController create() name Required str not "!" and "."
type Optional str "parent" or "child"
CellsController update() name Optional str not "!" and "."
type Optional str "parent" or "child"
CellsController sync_instances() project_id Optional NV
deleted Optional NV
updated_since Optional NV
ConsoleOutputController get_console_output() length Optional int Cast by int(str(length))
CoverageController _start_coverage() combine Optional bool boolean
CoverageController _report_coverage() file Optional str filename (if path != os.path.basename(path): Error)
xml Optional bool
html Optional bool
ServerDiskConfigController create() os-disk-config:disk_config Optional str "MANUAL" or "AUTO"
ServerDiskConfigController update() os-disk-config:disk_config Optional str "MANUAL" or "AUTO"
ServerDiskConfigController _action_rebuild() os-disk-config:disk_config Required str "MANUAL" or "AUTO"
ServerDiskConfigController _action_resize() os-disk-config:disk_config Required str "MANUAL" or "AUTO"
EvacuateController _evacuate() host Required NV
on_shared_storage Required str boolean
admin_password Optional NV
ExtendedVolumesController swap() old_volume_id Required str uuid
new_volume_id Required str uuid
ExtendedVolumesController attach() volume_id Required str uuid
device Required NV
ExtendedVolumesController detach() volume_id Required str uuid
FlavorActionController _add_tenant_access() tenant_id Required NV
FlavorActionController _remove_tenant_access() tenant_id Required NV
FlavorManageController _create() name Required str 1 - 255 names can only contain [a-zA-Z0-9_.- ]
id Required int or str 1 - 255 id can only contain [a-zA-Z0-9_.-]
ram Required int > 0
vcpus Required int > 0
disk Required int >= 0
ephemeral Optional int >= 0
swap Optional int >= 0
rxtx_factor Optional float > 0
os-flavor-access:is_public Optional str '1', 't', 'true', 'on', 'y', 'yes', '0', 'f', 'false', 'off', 'n', 'no'
FlavorExtraSpecsController create() extra_specs Optional dict
FlavorExtraSpecsController update() Optional dict
HostController update() status Required if not specifying maintenance_mode str "enable" or "disable"
maintenance_mode Required if not specifying status str "enable" or "disable"
KeypairController create() name Required str? (not type checked) 1 - 255
public_key Optional NV
MultinicController _add_fixed_ip() network_id Required NV
MultinicController _remove_fixed_ip() address Required NV
QuotaClassSetsController update() instances Optional int
cores Optional int
ram Optional int
security_groups Optional int
floating_ips Optional int
fixed_ips Optional int
metadata_items Optional int
injected_files Optional int
injected_file_content_bytes Optional int
injected_file_path_bytes Optional int
security_group_rules Optional int
key_pairs Optional int
QuotaSetsController update() instances Optional int
cores Optional int
ram Optional int
security_groups Optional int
floating_ips Optional int
fixed_ips Optional int
metadata_items Optional int
injected_files Optional int
injected_file_content_bytes Optional int
injected_file_path_bytes Optional int
security_group_rules Optional int
key_pairs Optional int
force Optional str boolean
RescueController _rescue() admin_pass Optional NV
SchedulerHintsController create() os-scheduler-hints:scheduler_hints Optional NV
ServerMetadataController create() metadata Required dict
ServerMetadataController update() metadata Required dict len(dict) == 1
ServerMetadataController update_all() metadata Required dict
ServersController create() admin_pass Optional str
name Required str 1 - 255
image_ref Required if not specifying os-block-device-mapping:block_device_mapping str uuid or ".*/<uuid>"
os-block-device-mapping:block_device_mapping Required if not specifying image_ref dict
source_type Required str 'volume', 'image', 'snapshot', 'blank'
uuid Required if source_type is not 'blank' NV
os-availability-zone:availability_zone Optional NV
os-config-drive:config_drive Optional NV
auto_disk_config Optional NV
key_name Optional NV
os-multiple-create:min_count Optional int >= 1
os-multiple-create:max_count Optional int >= 1
os-multiple-create:return_reservation_id Optional bool
personality Optional dict
path Required NV
contents Required str base64encoded
scheduler_hints Optional NV
os-security-groups:security_groups Optional list
name Optional str
os-user-data:user_data Optional NV
networks Optional list of dict
fixed_ip Optional, Should not specify both fixed_ip and port ipv4
port Optional, Should not specify both fixed_ip and port uuid
access_ip_v4 Optional ipv4
access_ip_v6 Optional ipv6
flavor_ref Required NV
metadata Optional {key: value}, 1 < len(key) < 255, len(value) < 255"
ServersController update() name Optional str 1 - 255
access_ip_v4 Optional ipv4
access_ip_v6 Optional ipv6
auto_disk_config Optional NV
ServersController _action_reboot() type Required str "HARD" or "SOFT", not allowed lower case.
ServersController _action_resize() flavor_ref Required NV validated with the existing flavors.
ServersController _action_rebuild() image_ref Required str uuid or ".*/<uuid>"
admin_pass Optional NV
name Optional str 1 - 255
access_ip_v4 Optional ipv4
access_ip_v6 Optional ipv6
auto_disk_config Optional NV
personality Optional NV
path Required NV
contents Required str base64encoded
ServersController _action_create_image() name Required NV
metadata Optional dict
ServiceController update() host Required NV
binary Required NV
disabled_reason Required if id is disable-log-reason str 1 - 255

Appendix: URL