| 39 | namespace container { |
| 40 | |
| 41 | Option<Error> validateContainerId(const ContainerID& containerId) |
| 42 | { |
| 43 | const string& id = containerId.value(); |
| 44 | |
| 45 | // Check common Mesos ID rules. |
| 46 | Option<Error> error = common::validation::validateID(id); |
| 47 | if (error.isSome()) { |
| 48 | return Error(error->message); |
| 49 | } |
| 50 | |
| 51 | // Check ContainerID specific rules. |
| 52 | // |
| 53 | // Valid the container id length |
| 54 | if (id.length() > MAX_CONTAINER_ID_LENGTH) { |
| 55 | return Error("'ContainerID.value' '" + id + "' exceeds the maximum" |
| 56 | " length (" + stringify(MAX_CONTAINER_ID_LENGTH) + ")"); |
| 57 | } |
| 58 | |
| 59 | // Periods are disallowed because our string representation of |
| 60 | // ContainerID uses periods: <uuid>.<child>.<grandchild>. |
| 61 | // For example: <uuid>.redis.backup |
| 62 | // |
| 63 | // Spaces are disallowed as they can render logs confusing and |
| 64 | // need escaping on terminals when dealing with paths. |
| 65 | auto invalidCharacter = [](char c) { |
| 66 | return c == '.' || c == ' '; |
| 67 | }; |
| 68 | |
| 69 | if (std::any_of(id.begin(), id.end(), invalidCharacter)) { |
| 70 | return Error("'ContainerID.value' '" + id + "'" |
| 71 | " contains invalid characters"); |
| 72 | } |
| 73 | |
| 74 | // TODO(bmahler): Print the invalid field nicely within the error |
| 75 | // (e.g. 'parent.parent.parent.value'). For now we only have one |
| 76 | // level of nesting so it's ok. |
| 77 | if (containerId.has_parent()) { |
| 78 | Option<Error> parentError = validateContainerId(containerId.parent()); |
| 79 | |
| 80 | if (parentError.isSome()) { |
| 81 | return Error("'ContainerID.parent' is invalid: " + parentError->message); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return None(); |
| 86 | } |
| 87 | |
| 88 | } // namespace container { |
| 89 | |