« App/Dochazka/REST/Docs/Workflow.pm
↑ Summary
auto/share/dist/App-Dochazka-REST/Component_Config.pm »
| 1 | # ************************************************************************* | ||
| 2 | # Copyright (c) 2014-2015, SUSE LLC | ||
| 3 | # | ||
| 4 | # All rights reserved. | ||
| 5 | # | ||
| 6 | # Redistribution and use in source and binary forms, with or without | ||
| 7 | # modification, are permitted provided that the following conditions are met: | ||
| 8 | # | ||
| 9 | # 1. Redistributions of source code must retain the above copyright notice, | ||
| 10 | # this list of conditions and the following disclaimer. | ||
| 11 | # | ||
| 12 | # 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | # notice, this list of conditions and the following disclaimer in the | ||
| 14 | # documentation and/or other materials provided with the distribution. | ||
| 15 | # | ||
| 16 | # 3. Neither the name of SUSE LLC nor the names of its contributors may be | ||
| 17 | # used to endorse or promote products derived from this software without | ||
| 18 | # specific prior written permission. | ||
| 19 | # | ||
| 20 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 21 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 22 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 23 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 24 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 25 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 26 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 27 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 28 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 29 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 30 | # POSSIBILITY OF SUCH DAMAGE. | ||
| 31 | # ************************************************************************* | ||
| 32 | |||
| 33 | package App::Dochazka::REST::Guide; | ||
| 34 | |||
| 35 | use 5.012; | ||
| 36 | use strict; | ||
| 37 | use warnings; | ||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | =head1 NAME | ||
| 42 | |||
| 43 | App::Dochazka::REST::Guide - Dochazka REST server guide | ||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | =head1 SYNOPSIS | ||
| 48 | |||
| 49 | This POD-only module describes the Dochazka REST server (API) in more detail. | ||
| 50 | |||
| 51 | Dochazka as a whole aims to be a convenient, open-source ATT solution. | ||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | =head1 ARCHITECTURE | ||
| 56 | |||
| 57 | Dochazka consists of three main components: | ||
| 58 | |||
| 59 | =over | ||
| 60 | |||
| 61 | =item * REST server (this module) | ||
| 62 | |||
| 63 | The REST server listens for and processes incoming HTTP requests. Processing includes | ||
| 64 | authentication and authorization. The server attempts to map the request URI to a | ||
| 65 | Dochazka resource. The resource handler takes action on the request, depending on the | ||
| 66 | HTTP method (GET, PUT, POST, DELETE). Typically, this action will culminate in | ||
| 67 | one or more SQL statements which are sent to the PostgreSQL database for | ||
| 68 | execution. The results are sent back to the client in the HTTP reponse. | ||
| 69 | |||
| 70 | =item * PostgreSQL database | ||
| 71 | |||
| 72 | The PostgreSQL database is configured to listen for incoming SQL statements | ||
| 73 | from the REST server. Based on these statements, it creates, retrieves, | ||
| 74 | updates, and deletes (CRUD) employee attendance records and related data in the | ||
| 75 | Dochazka database. | ||
| 76 | |||
| 77 | =item * Dochazka clients | ||
| 78 | |||
| 79 | Dochazka clients, such as L<App::Dochazka::WWW>, L<App::Dochazka::CLI>, and | ||
| 80 | perhaps others, present a user interface (UI) to employees, by which they try | ||
| 81 | to divine their intent and express it in terms of HTTP requests to the REST | ||
| 82 | server. | ||
| 83 | |||
| 84 | The HTTP protocol is used in all communication between client and server. In | ||
| 85 | Dochazka, the term "client" should be understood in a broad sense to mean | ||
| 86 | anything that communicates with the server using the HTTP protocol. This | ||
| 87 | encompasses stand-alone report generators, specialized administration | ||
| 88 | utilities, cronjobs, web browsers, etc., in addition to the purpose-built | ||
| 89 | clients or just plain C<curl>. | ||
| 90 | |||
| 91 | =back | ||
| 92 | |||
| 93 | |||
| 94 | |||
| 95 | =head1 INSTALLATION | ||
| 96 | |||
| 97 | Installation is the process of creating (setting up, bootstrapping) a new | ||
| 98 | Dochazka instance, or "site" in Dochazka terminology. | ||
| 99 | |||
| 100 | It entails the following steps. | ||
| 101 | |||
| 102 | |||
| 103 | =head2 Server preparation | ||
| 104 | |||
| 105 | Dochazka REST needs hardware (either physical or virtualized) to run on. | ||
| 106 | The hardware will need to have a network connection, etc. Obviously, this | ||
| 107 | step is entirely beyond the scope of this document. | ||
| 108 | |||
| 109 | =head2 Software installation | ||
| 110 | |||
| 111 | Once the hardware is ready, the Dochazka REST software and all its | ||
| 112 | dependencies are installed on it. This could be accomplished by | ||
| 113 | downloading and unpacking the tarball (or running C<git clone>) and | ||
| 114 | following the installation instructions, or, more expediently, by | ||
| 115 | installing a packaged version of Dochazka REST if one is available | ||
| 116 | (see | ||
| 117 | L<https://build.opensuse.org/package/show/home:smithfarm/perl-App-Dochazka-REST>). | ||
| 118 | |||
| 119 | =head2 PostgreSQL setup | ||
| 120 | |||
| 121 | One of Dochazka REST's principal dependencies is PostgreSQL server (version | ||
| 122 | 9.2 or higher). This needs to be installed (should happen automatically | ||
| 123 | when using the packaged version of L<App::Dochazka::REST>). Steps to enable | ||
| 124 | it: | ||
| 125 | |||
| 126 | bash# systemctl enable postgresql.service | ||
| 127 | bash# systemctl start postgresql.service | ||
| 128 | bash# su - postgres | ||
| 129 | bash$ psql postgres | ||
| 130 | postgres-# ALTER ROLE postgres WITH PASSWORD 'mypass'; | ||
| 131 | ALTER ROLE | ||
| 132 | |||
| 133 | At this point, we exit C<psql> and, still as the user C<postgres>, we | ||
| 134 | edit C<pg_hba.conf>. In SUSE distributions, this file is located in | ||
| 135 | C<data/> under the C<postgres> home directory. Using our favorite editor, | ||
| 136 | we change the METHOD entry for C<local> so it looks like this: | ||
| 137 | |||
| 138 | # TYPE DATABASE USER ADDRESS METHOD | ||
| 139 | local all all password | ||
| 140 | |||
| 141 | For the audit triggers to work (and the application will not run otherwise), we | ||
| 142 | must to add the following line to the end of C<postgresql.conf> (also | ||
| 143 | located in C<data/> in SUSE distros): | ||
| 144 | |||
| 145 | dochazka.eid = -1 | ||
| 146 | |||
| 147 | Then, as root, we restart the postgresql service: | ||
| 148 | |||
| 149 | bash# systemctl restart postgresql.service | ||
| 150 | |||
| 151 | Lastly, check if you can connect to the C<postgres> database using the password: | ||
| 152 | |||
| 153 | bash$ psql --username postgres postgres | ||
| 154 | Password for user postgres: [...type 'mypass'...] | ||
| 155 | psql (9.2.7) | ||
| 156 | Type "help" for help. | ||
| 157 | |||
| 158 | postgres=# | ||
| 159 | |||
| 160 | To exit, type C<\q> at the postgres prompt: | ||
| 161 | |||
| 162 | postgres=# \q | ||
| 163 | bash$ | ||
| 164 | |||
| 165 | |||
| 166 | =head2 Site configuration | ||
| 167 | |||
| 168 | Before the Dochazka REST database can be initialized, we will need to | ||
| 169 | tell L<App::Dochazka::REST> about the PostgreSQL superuser password | ||
| 170 | that we set in the previous step. This is done via a site parameter. | ||
| 171 | There may be other site params we will want to set, but the following | ||
| 172 | is sufficient to run the test suite. | ||
| 173 | |||
| 174 | First, create a sitedir: | ||
| 175 | |||
| 176 | bash# mkdir /etc/dochazka-rest | ||
| 177 | |||
| 178 | and, second, a file therein: | ||
| 179 | |||
| 180 | # cat << EOF > /etc/dochazka-rest/REST_SiteConfig.pm | ||
| 181 | set( 'MREST_DEBUG_MODE', 1 ); | ||
| 182 | set( 'DBINIT_CONNECT_SUPERAUTH', 'mypass' ); | ||
| 183 | set( 'DOCHAZKA_REST_LOG_FILE', "dochazka-rest.log" ); | ||
| 184 | set( 'DOCHAZKA_REST_LOG_FILE_RESET', 1); | ||
| 185 | EOF | ||
| 186 | # | ||
| 187 | |||
| 188 | Where 'mypass' is the PostgreSQL password you set in the 'ALTER | ||
| 189 | ROLE' command, above. | ||
| 190 | |||
| 191 | The C<DBINIT_CONNECT_SUPERAUTH> setting is only needed for database | ||
| 192 | initialization (see below), when L<App::Dochazka::REST> connects to PostgreSQL | ||
| 193 | as user 'postgres' to drop/create the database. Once the database is created, | ||
| 194 | L<App::Dochazka::REST> connects to it using the PostgreSQL credentials of the | ||
| 195 | current user. | ||
| 196 | |||
| 197 | |||
| 198 | =head2 Database initialization | ||
| 199 | |||
| 200 | To initialize the database or reset it to a pristine state: | ||
| 201 | |||
| 202 | $ dochazka-resetdb | ||
| 203 | |||
| 204 | Note that this is a two-step process. The first step is to create the database, | ||
| 205 | role, extensions etc. - i.e., everything that requires database superuser | ||
| 206 | permissions. The second step is to create the schemas, etc. For this, the | ||
| 207 | ordinary "dochazka" role is used. | ||
| 208 | |||
| 209 | In a production setting, or whenever the two steps need to be done separately, | ||
| 210 | the database administrator can perform the first step using the "psql" command | ||
| 211 | in C<bin/dochazka-resetdb>. After that, the second step can be performed by | ||
| 212 | simply running | ||
| 213 | |||
| 214 | $ dochazka-dbinit | ||
| 215 | |||
| 216 | |||
| 217 | =head2 Start the server | ||
| 218 | |||
| 219 | The last step is to start the Dochazka REST server. In the future, this | ||
| 220 | will be possible using a command like C<systemctl start dochazka-rest.service>. | ||
| 221 | At the moment, however, we are still in development/testing phase and we | ||
| 222 | start the server like this: | ||
| 223 | |||
| 224 | $ dochazka-rest | ||
| 225 | Starting Web::MREST ver. 0.282 | ||
| 226 | App distro is App-Dochazka-REST | ||
| 227 | App module is App::Dochazka::REST::Dispatch | ||
| 228 | Distro sharedir is | ||
| 229 | /usr/lib/perl5/site_perl/5.18.2/auto/share/dist/App-Dochazka-REST | ||
| 230 | Local site configuration directory is /etc/dochazka-rest | ||
| 231 | Loading configuration parameters from /etc/dochazka-rest | ||
| 232 | Setting up logging | ||
| 233 | Logging to /home/smithfarm/mrest.log | ||
| 234 | Calling App::Dochazka::REST::Dispatch::init() | ||
| 235 | Starting server | ||
| 236 | HTTP::Server::PSGI: Accepting connections at http://0:5000/ | ||
| 237 | |||
| 238 | Note that the development web server L<HTTP::Server::PSGI> is used. To use | ||
| 239 | L<Starman> instead, use the following command: | ||
| 240 | |||
| 241 | $ dochazka-rest -- --server Starman | ||
| 242 | |||
| 243 | |||
| 244 | =head2 Take it for a spin | ||
| 245 | |||
| 246 | Point your browser to L<http://localhost:5000/> | ||
| 247 | |||
| 248 | |||
| 249 | |||
| 250 | =head1 BASIC PARAMETERS | ||
| 251 | |||
| 252 | =head2 UTF-8 | ||
| 253 | |||
| 254 | The server assumes all incoming requests are encoded in UTF-8, and it encodes | ||
| 255 | all of its responses in UTF-8 as well. | ||
| 256 | |||
| 257 | =head2 HTTP(S) | ||
| 258 | |||
| 259 | In order to protect user passwords from network sniffing and other nefarious | ||
| 260 | activities, it is recommended that the server be set up to accept HTTPS | ||
| 261 | requests only. | ||
| 262 | |||
| 263 | =head2 Self-documenting | ||
| 264 | |||
| 265 | Another implication of REST is that the server provides "resources" and that | ||
| 266 | those resources are, to some extent at least, self-documenting. | ||
| 267 | |||
| 268 | |||
| 269 | |||
| 270 | =head1 EXPLORING THE SERVER | ||
| 271 | |||
| 272 | =head2 With a web browser | ||
| 273 | |||
| 274 | Some resources (those that use the GET method) are accessible using a web | ||
| 275 | browser. That said, if we are only interested in displaying information | ||
| 276 | from the database, GET requests are all we need and using a web browser can | ||
| 277 | be convenient. | ||
| 278 | |||
| 279 | To start exploring, fire up a standard web browser and point it to the base URI | ||
| 280 | of your L<App::Dochazka::REST> installation: | ||
| 281 | |||
| 282 | http://dochazka.site | ||
| 283 | |||
| 284 | and entering one's credentials in the Basic Authentication dialog. | ||
| 285 | |||
| 286 | =head2 With a command-line HTTP client | ||
| 287 | |||
| 288 | To access all the resources, you will need a client that is capable of | ||
| 289 | generating POST, PUT, and DELETE requests as well as GET requests. Also, since | ||
| 290 | some of the information L<App::Dochazka::REST> provides is in the response | ||
| 291 | headers, the client needs to be capable of displaying those as well. | ||
| 292 | |||
| 293 | One such client is Daniel Stenberg's B<curl>. | ||
| 294 | |||
| 295 | In the HTTP request, the client may provide an C<Accept:> header specifying | ||
| 296 | either HTML (C<text/html>) or JSON (C<application/json>). For the convenience | ||
| 297 | of those using a web browser, HTML is the default. | ||
| 298 | |||
| 299 | Here are some examples of how to use B<curl> (or a web browser) to explore | ||
| 300 | resources. These examples assume a vanilla installation of | ||
| 301 | L<App::Dochazka::REST> with the default root password. The same commands can be | ||
| 302 | used with a production server, but keep in mind that the resources you will see | ||
| 303 | may be limited by your privilege level. | ||
| 304 | |||
| 305 | =over | ||
| 306 | |||
| 307 | =item * GET resources | ||
| 308 | |||
| 309 | The GET method is used to search for and display information. The top-level | ||
| 310 | GET resources are listed at the top-level URI, either using B<curl> | ||
| 311 | |||
| 312 | $ curl -v -H 'Accept: application/json' http://demo:demo@dochazka.site/ | ||
| 313 | |||
| 314 | Similarly, to display a list of sub-resources under the 'privhistory' top-level | ||
| 315 | resource, enter the command: | ||
| 316 | |||
| 317 | $ curl http://demo:demo@dochazka.site/employee -H 'Accept: application/json' | ||
| 318 | |||
| 319 | Oops - no resources are displayed because the 'demo' user has only passerby | ||
| 320 | privileges, but all the privhistory resources require at least 'active'. To | ||
| 321 | see all the available resources, we can authenticate as 'root': | ||
| 322 | |||
| 323 | $ curl http://root:immutable@dochazka.site/employee -H 'Accept: application/json' | ||
| 324 | |||
| 325 | =item * POST resources | ||
| 326 | |||
| 327 | With the GET method, we could only access resources for finding and displaying | ||
| 328 | information: we could not add, change, or delete information. For that we will | ||
| 329 | need to turn to some other client than the web browser -- a client like B<curl> | ||
| 330 | that is capable of generating HTTP requests with methods like POST (as well as | ||
| 331 | PUT and DELETE). | ||
| 332 | |||
| 333 | Here is an example of how we would use B<curl> to display the top-level POST | ||
| 334 | resources: | ||
| 335 | |||
| 336 | curl -v http://root:immutable@dochazka.site -X POST -H "Content-Type: application/json" | ||
| 337 | |||
| 338 | The "Content-Type: application/json" header is necessary because the server | ||
| 339 | only accepts JSON in the POST request body -- even though in this case we | ||
| 340 | did not send a request body, most POST requests will have one. For best | ||
| 341 | results, the request body should be a legal JSON string represented as a | ||
| 342 | sequence of bytes encoded in UTF-8. | ||
| 343 | |||
| 344 | =item * PUT resources | ||
| 345 | |||
| 346 | The PUT method is used to add new resources and update existing ones. Since | ||
| 347 | the resources are derived from the underlying database, this implies executing | ||
| 348 | INSERT and UPDATE statements on tables in the database. | ||
| 349 | |||
| 350 | PUT resources can be explored using a B<curl> command analogous to the one | ||
| 351 | given for the POST method. | ||
| 352 | |||
| 353 | =item * DELETE resources | ||
| 354 | |||
| 355 | Any time we need to delete information -- i.e., completely wipe it from | ||
| 356 | the database, we will need to use the DELETE method. | ||
| 357 | |||
| 358 | DELETE resources can be explored using a B<curl> command analogous to the one | ||
| 359 | given for the POST method. | ||
| 360 | |||
| 361 | Keep in mind that the data integrity constraints in the underlying PostgreSQL | ||
| 362 | database may make it difficult to delete a resource if any other resources | ||
| 363 | are linked to it. For example, an employee cannot be deleted until all | ||
| 364 | intervals, privhistory records, schedhistory records, locks, etc. linked to | ||
| 365 | that employee have been deleted. Intervals, on the other hand, can be | ||
| 366 | deleted as long as they are not subject to a lock. | ||
| 367 | |||
| 368 | =back | ||
| 369 | |||
| 370 | |||
| 371 | =head1 DOCUMENTATION OF REST RESOURCES | ||
| 372 | |||
| 373 | The definition of each resource includes an HTML string containing the | ||
| 374 | resource's documentation. This string can be accessed via POST request for | ||
| 375 | the C<docu> resource (provide the resource name in double quotes in the | ||
| 376 | request body). | ||
| 377 | |||
| 378 | In order to be "self-documenting", the definition of each REST resource | ||
| 379 | contains a "short" description and a "long" POD string. From time to time, the | ||
| 380 | entire resource tree is walked to generate a module, | ||
| 381 | L<App::Dochazka::REST::Docs::Resources>, containing all the resource | ||
| 382 | documentation. | ||
| 383 | |||
| 384 | |||
| 385 | |||
| 386 | =head1 REQUEST-RESPONSE CYCLE | ||
| 387 | |||
| 388 | Incoming HTTP requests are handled by L<App::Dochazka::REST::Resource>, | ||
| 389 | which inherits from L<Web::Machine::Resource>. The latter uses L<Plack> to | ||
| 390 | implement a PSGI-compliant stack. | ||
| 391 | |||
| 392 | L<Web::Machine> takes a "state-machine" approach to implementing the HTTP 1.1 | ||
| 393 | standard. Requests are processed by running them through a state | ||
| 394 | machine, each "cog" of which is a L<Web::Machine::Resource> method that can | ||
| 395 | be overridden by a child module. In our case, this module is | ||
| 396 | L<App::Dochazka::REST::Resource>. | ||
| 397 | |||
| 398 | The behavior of the resulting web server can be characterized as follows: | ||
| 399 | |||
| 400 | =over | ||
| 401 | |||
| 402 | =item * B<Allowed methods test> | ||
| 403 | |||
| 404 | One of the first things the server looks at, when it receives a request, is | ||
| 405 | the method. Only certain HTTP methods, such as 'GET' and 'POST', are accepted. | ||
| 406 | If this test fails, a "405 Method Not Allowed" response is sent. | ||
| 407 | |||
| 408 | =item * B<Internal and external authentication, session management> | ||
| 409 | |||
| 410 | This takes place when L<Web::Machine> calls the C<is_authorized> method, | ||
| 411 | our implementation of which is in L<App::Dochazka::REST::Auth>. | ||
| 412 | |||
| 413 | Though the method is called C<is_authorized>, what it really does is | ||
| 414 | authenticate the request - i.e., validate the user's credentials to | ||
| 415 | determine his or her identity. B<Authorization> - determination whether the | ||
| 416 | user has sufficient privileges to make the request - takes place one step | ||
| 417 | further on. (The HTTP standard uses the term "authorized" to mean | ||
| 418 | "authenticated"; the name of this method is a nod to that usage.) | ||
| 419 | |||
| 420 | In C<is_authorized>, the user's credentials are authenticated | ||
| 421 | against an external database (LDAP), an internal database (PostgreSQL | ||
| 422 | 'employees' table), or both. Session management techniques are utilized | ||
| 423 | to minimize external authentication queries, which impose latency. The | ||
| 424 | authentication and session management algorithms are described in | ||
| 425 | L<"AUTHENTICATION AND SESSION MANAGEMENT">. If authentication fails, a "401 | ||
| 426 | Unauthorized" response is sent. | ||
| 427 | |||
| 428 | Since this is the first time that the PostgreSQL database is needed, this | ||
| 429 | is also where the L<DBIx::Connector> object is attached to the request | ||
| 430 | context. (The request context is a hashref that accompanies the request | ||
| 431 | as it undergoes processing.) For details, see | ||
| 432 | L<App::Dochazka::REST::Auth/"is_authorized">. | ||
| 433 | |||
| 434 | In a web browser, repeated failed authentication attempts are typically | ||
| 435 | associated with repeated display of the credentials dialog (and no other | ||
| 436 | indication of what is wrong, which can be confusing to users but is probably a | ||
| 437 | good idea, because any error messages could be abused by attackers). | ||
| 438 | |||
| 439 | =item * B<Authorization/ACL check> | ||
| 440 | |||
| 441 | After the request is authenticated (associated with a known employee), the | ||
| 442 | server examines the ACL profile of the resource being requested and compares it | ||
| 443 | with the employee's privilege level. If the privilege level is too low for the | ||
| 444 | requested operation, a "403 Forbidden" response is sent. | ||
| 445 | |||
| 446 | The ACL profile is part of the resource definition. It can be specified either | ||
| 447 | as a single value for all HTTP methods, or as a hash, e.g.: | ||
| 448 | |||
| 449 | { | ||
| 450 | GET => 'passerby', | ||
| 451 | PUT => 'admin', | ||
| 452 | DELETE => 'admin', | ||
| 453 | } | ||
| 454 | |||
| 455 | In certain operations (i.e., combinations of HTTP method and resource), the | ||
| 456 | full range of functionality may be available only to administrators. See These | ||
| 457 | operations are special cases. Their ACL profile is either 'inactive' or | ||
| 458 | 'active', but a non-administrator employee may still get a 403 Forbidden error | ||
| 459 | on the operation if they are trying to do something, such as update an interval | ||
| 460 | belonging to a different employee, that is reserved for administrators. | ||
| 461 | |||
| 462 | =item * B<Test for resource existence> | ||
| 463 | |||
| 464 | The next test a request undergoes on its quest to become a response is the | ||
| 465 | test of resource existence. If the request is asking for a non-existent resource, | ||
| 466 | e.g. L<http://dochazka.site/employee/curent>, it cannot be fulfilled and a "404 | ||
| 467 | Not Found" response will be sent. | ||
| 468 | |||
| 469 | For GET requests, this is ordinarily the last cog in the state machine: if the | ||
| 470 | test passes, a "200 OK" response is typically sent, along with a response body. | ||
| 471 | (There are exceptions to this rule, however - see L<the AUTHORIZATION | ||
| 472 | chapter|"AUTHORIZATION">.) Requests using other methods (POST, PUT, DELETE) are | ||
| 473 | subject to further processing as described below. | ||
| 474 | |||
| 475 | =back | ||
| 476 | |||
| 477 | =head2 Additional processing (POST and PUT) | ||
| 478 | |||
| 479 | Because they are expected to have a request body, incoming POST and PUT | ||
| 480 | requests are subject to the following additional test: | ||
| 481 | |||
| 482 | =over | ||
| 483 | |||
| 484 | =item * B<malformed_request> | ||
| 485 | |||
| 486 | This test examines the request body. If it is non-existent, the test | ||
| 487 | passes. If the body exists and is valid JSON, the test passes. Otherwise, | ||
| 488 | it fails. | ||
| 489 | |||
| 490 | =item * B<known_content_type> | ||
| 491 | |||
| 492 | Test the request for the 'Content-Type' header. POST and PUT requests | ||
| 493 | should have a header that says: | ||
| 494 | |||
| 495 | Content-Type: application/json | ||
| 496 | |||
| 497 | If this header is not present, a "415 Unsupported Media Type" response is | ||
| 498 | sent. | ||
| 499 | |||
| 500 | =back | ||
| 501 | |||
| 502 | =head2 Additional processing (POST) | ||
| 503 | |||
| 504 | =over | ||
| 505 | |||
| 506 | #=item * B<post_is_create> | ||
| 507 | # | ||
| 508 | #This test examines the POST request and places it into one of two | ||
| 509 | #categories: (1) generic request for processing, (2) a request that creates | ||
| 510 | #or otherwise manipulates a resource. | ||
| 511 | |||
| 512 | =back | ||
| 513 | |||
| 514 | |||
| 515 | =head1 DATA MODEL | ||
| 516 | |||
| 517 | This section describes the C<App::Dochazka::REST> data model. Conceptually, | ||
| 518 | Dochazka data can be seen to exist in the following classes of objects: | ||
| 519 | |||
| 520 | =over | ||
| 521 | |||
| 522 | ##=item * Policy (parameters set when database is first created) | ||
| 523 | ## | ||
| 524 | =item * Employee (an individual employee) | ||
| 525 | |||
| 526 | =item * Privhistory (history of changes in an employee's privilege level) | ||
| 527 | |||
| 528 | =item * Schedule (a schedule) | ||
| 529 | |||
| 530 | =item * Schedhistory (history of changes in an employee's schedule) | ||
| 531 | |||
| 532 | =item * Activities (what kinds of work are recognized) | ||
| 533 | |||
| 534 | =item * Intervals (the "work", or "attendance", itself) | ||
| 535 | |||
| 536 | =item * Locks (determining whether a reporting period is locked or not) | ||
| 537 | |||
| 538 | =item * Components (Mason components, i.e. report templates) | ||
| 539 | |||
| 540 | =back | ||
| 541 | |||
| 542 | The "state" of each object is stored in a PostgreSQL database (see | ||
| 543 | L<"DATABASE"> for details). | ||
| 544 | |||
| 545 | These classes are described in the following sections. | ||
| 546 | |||
| 547 | =for comment | ||
| 548 | ##=head2 Policy | ||
| 549 | ## | ||
| 550 | ##Dochazka is configurable in a number of ways. Some configuration parameters | ||
| 551 | ##are set once at installation time and, once set, can never be changed -- | ||
| 552 | ##these are referred to as "site policy" parameters. Others, referred to as | ||
| 553 | ##"site configuration parameters" or "site params", are set in configuration | ||
| 554 | ##files such as C<Dochazka_SiteConfig.pm> (see L</SITE CONFIGURATION>) and | ||
| 555 | ##can be changed more-or-less at will. | ||
| 556 | ## | ||
| 557 | ##The key difference between site policy and site configuration is that | ||
| 558 | ##site policy parameters cannot be changed, because changing them would | ||
| 559 | ##compromise the referential integrity of the underlying database. | ||
| 560 | ## | ||
| 561 | ##Site policy parameters are set at installation time and are stored, as a | ||
| 562 | ##single JSON string, in the C<SitePolicy> table. This table is rendered | ||
| 563 | ##effectively immutable by a trigger. | ||
| 564 | ## | ||
| 565 | ##For details, see L<App::Dochazka::REST::Model::Policy>. | ||
| 566 | |||
| 567 | |||
| 568 | =head2 Employee | ||
| 569 | |||
| 570 | Users of Dochazka are referred to as "employees" regardless of their | ||
| 571 | legal status -- in reality they might be independent contractors, or | ||
| 572 | students, or even household pets, but as far as Dochazka is concerned they | ||
| 573 | are employees. You could say that "employee" is the Dochazka term for "user". | ||
| 574 | |||
| 575 | The purpose of the Employee table/object is to store whatever data the site | ||
| 576 | is accustomed to use to identify its employees. | ||
| 577 | |||
| 578 | Within Dochazka itself, employees are distinguished by an internal employee ID | ||
| 579 | number (EID), which is assigned by Dochazka itself when the employee record is | ||
| 580 | created. In addition, four other fields/properties are provided to identify | ||
| 581 | the employee: | ||
| 582 | |||
| 583 | =over | ||
| 584 | |||
| 585 | =item * nick | ||
| 586 | |||
| 587 | =item * sec_id | ||
| 588 | |||
| 589 | =item * fullname | ||
| 590 | |||
| 591 | =item * email | ||
| 592 | |||
| 593 | =back | ||
| 594 | |||
| 595 | All four of these, plus the C<eid> field, have C<UNIQUE> constraints defined at | ||
| 596 | the database level, meaning that duplicate entries are not permitted. However, | ||
| 597 | of the four, only C<nick> is required. | ||
| 598 | |||
| 599 | Depending on how authentication is set up, employee passwords may also be | ||
| 600 | stored in this table, using the C<passhash> and C<salt> fields. | ||
| 601 | |||
| 602 | For details, see L<App::Dochazka::REST::Model::Employee>. | ||
| 603 | |||
| 604 | |||
| 605 | =head2 Privhistory | ||
| 606 | |||
| 607 | Dochazka has four privilege levels: C<admin>, C<active>, C<inactive>, and | ||
| 608 | C<passerby>: | ||
| 609 | |||
| 610 | =over | ||
| 611 | |||
| 612 | =item * C<admin> -- employee can view, modify, and place/remove locks on her | ||
| 613 | own attendance data as well as that of other employees; she can also | ||
| 614 | administer employee accounts and set privilege levels of other employees | ||
| 615 | |||
| 616 | =item * C<active> -- employee can view her own profile, attendance data, | ||
| 617 | modify her own unlocked attendance data, and place locks on her attendance | ||
| 618 | data | ||
| 619 | |||
| 620 | =item * C<inactive> -- employee can view her own profile and attendance data | ||
| 621 | |||
| 622 | =item * C<passerby> -- employee can view her own profile | ||
| 623 | |||
| 624 | =back | ||
| 625 | |||
| 626 | Dochazka's C<privhistory> object is used to track changes in an employee's | ||
| 627 | privilege level over time. Each time an employee's privilege level changes, | ||
| 628 | a Dochazka administrator (i.e., an employee whose current privilege level is | ||
| 629 | 'admin'), a record is inserted into the database (in the C<privhistory> | ||
| 630 | table). Ordinary employees (i.e. those whose current privilege level is | ||
| 631 | 'active') can read their own privhistory. | ||
| 632 | |||
| 633 | Thus, with Dochazka it is possible not only to determine not only an employee's | ||
| 634 | current privilege level, but also to view "privilege histories" and to | ||
| 635 | determine employees' privilege levels for any date (timestamp) in the past. | ||
| 636 | |||
| 637 | For details, see L<App::Dochazka::REST::Model::Privhistory> and L<When | ||
| 638 | history changes take effect>. | ||
| 639 | |||
| 640 | |||
| 641 | =head2 Schedule | ||
| 642 | |||
| 643 | In addition to actual attendance data, Dochazka sites may need to store | ||
| 644 | schedules. Dochazka defines the term "schedule" as a series of | ||
| 645 | non-overlapping "time intervals" (or "timestamp ranges" in PostgreSQL | ||
| 646 | terminology) falling within a single week. These time intervals express the | ||
| 647 | times when the employee is "expected" or "supposed" to work (or be "at work") | ||
| 648 | during the scheduling period. | ||
| 649 | |||
| 650 | Example: employee "Barb" is on a weekly schedule. That means her | ||
| 651 | scheduling period is "weekly" and her schedule is an array of | ||
| 652 | non-overlapping time intervals, all falling within a single week. | ||
| 653 | |||
| 654 | B<In its current form, Dochazka is only capable of handling weekly schedules | ||
| 655 | only.> Some sites, such as hospitals, nuclear power plants, fire departments, | ||
| 656 | and the like, might have employees on more complicated schedules such as "one | ||
| 657 | week on, one week off", alternating day and night shifts, "on call" duty, etc. | ||
| 658 | |||
| 659 | Dochazka can still be used to track attendance of such employees, but if their | ||
| 660 | work schedule cannot be expressed as a series of non-overlapping time intervals | ||
| 661 | contained within a contiguous 168-hour period (i.e. one week), then their | ||
| 662 | Dochazka schedule should be set to NULL. | ||
| 663 | |||
| 664 | For details, see L<App::Dochazka::REST::Model::Schedule>. | ||
| 665 | |||
| 666 | |||
| 667 | =head2 Schedhistory | ||
| 668 | |||
| 669 | The C<schedhistory> table contains a historical record of changes in the | ||
| 670 | employee's schedule. This makes it possible to determine an employee's | ||
| 671 | schedule for any date (timestamp) in the past, as well as (crucially) the | ||
| 672 | employee's current schedule. | ||
| 673 | |||
| 674 | Every time an employee's schedule is to change, a Dochazka administrator | ||
| 675 | must insert a record into this table. (Employees who are not administrators | ||
| 676 | can only read their own history; they do not have write privileges.) For | ||
| 677 | more information on privileges, see L</AUTHORIZATION>. | ||
| 678 | |||
| 679 | For details, see L<App::Dochazka::REST::Model::Schedhistory>. | ||
| 680 | |||
| 681 | |||
| 682 | =head2 Activity | ||
| 683 | |||
| 684 | While on the job, employees "work" -- i.e., they engage in various activities | ||
| 685 | that are tracked using Dochazka. The C<activities> table contains definitions | ||
| 686 | of all the possible activities that may be entered in the C<intervals> table. | ||
| 687 | |||
| 688 | The initial set of activities is defined in the site install configuration | ||
| 689 | (C<DOCHAZKA_ACTIVITY_DEFINITIONS>) and enters the database at installation | ||
| 690 | time. Additional activities can be added later (by administrators), but | ||
| 691 | activities can be deleted only if no intervals refer to them. | ||
| 692 | |||
| 693 | Each activity has a code, or short name (e.g., "WORK") -- which is the | ||
| 694 | primary way of referring to the activity -- as well as an optional long | ||
| 695 | description. Activity codes must be all upper-case. | ||
| 696 | |||
| 697 | For details, see L<App::Dochazka::REST::Model::Activity>. | ||
| 698 | |||
| 699 | |||
| 700 | =head2 Interval | ||
| 701 | |||
| 702 | Intervals are the heart of Dochazka's attendance data. For Dochazka, an | ||
| 703 | interval is an amount of time that an employee spends doing an activity. | ||
| 704 | In the database, intervals are represented using the C<tsrange> range | ||
| 705 | operator introduced in PostgreSQL 9.2. | ||
| 706 | |||
| 707 | Optionally, an interval can have a C<long_desc> (employee's description | ||
| 708 | of what she did during the interval) and a C<remark> (admin remark). | ||
| 709 | |||
| 710 | For details, see L<App::Dochazka::REST::Model::Interval>. | ||
| 711 | |||
| 712 | |||
| 713 | =head2 Lock | ||
| 714 | |||
| 715 | In Dochazka, a "lock" is a record in the "locks" table specifying that | ||
| 716 | a particular user's attendance data (i.e. activity intervals) for a | ||
| 717 | given period (tsrange) cannot be changed. That means, for intervals in | ||
| 718 | the locked tsrange: | ||
| 719 | |||
| 720 | =over | ||
| 721 | |||
| 722 | =item * existing intervals cannot be updated or deleted | ||
| 723 | |||
| 724 | =item * no new intervals can be inserted | ||
| 725 | |||
| 726 | =back | ||
| 727 | |||
| 728 | Employees can create locks (i.e., insert records into the locks table) on their | ||
| 729 | own EID, but they cannot delete or update those locks (or any others). | ||
| 730 | Administrators can insert, update, or delete locks at will. | ||
| 731 | |||
| 732 | How the lock is used will differ from site to site, and some sites may not | ||
| 733 | even use locking at all. The typical use case would be to lock all the | ||
| 734 | employee's attendance data within the given period as part of pre-payroll | ||
| 735 | processing. For example, the Dochazka client application may be set up to | ||
| 736 | enable reports to be generated only on fully locked periods. | ||
| 737 | |||
| 738 | "Fully locked" means either that a single lock record has been inserted | ||
| 739 | covering the entire period, or that the entire period is covered by multiple | ||
| 740 | locks. | ||
| 741 | |||
| 742 | Any attempts (even by administrators) to enter activity intervals that | ||
| 743 | intersect an existing lock will result in an error. | ||
| 744 | |||
| 745 | Clients can of course make it easy for the employee to lock entire blocks | ||
| 746 | of time (weeks, months, years . . .) at once, if that is deemed expedient. | ||
| 747 | |||
| 748 | For details, see L<App::Dochazka::REST::Model::Lock>. | ||
| 749 | |||
| 750 | |||
| 751 | =head2 Component | ||
| 752 | |||
| 753 | L<Reports are generated|"REPORT GENERATION"> from | ||
| 754 | L<Mason|https://metacpan.org/pod/Mason> templates which consist of | ||
| 755 | components. Mason expects these components to be stored in text files under | ||
| 756 | a directory called the "component root". For the purposes of Dochazka, the | ||
| 757 | component root is created under the Dochazka state directory, which is | ||
| 758 | determined from the C<DOCHAZKA_STATE_DIR> site parameter (defaults to | ||
| 759 | C</var/lib/dochazka>). When the server starts, this Mason state in the | ||
| 760 | filesystem is wiped and re-created from the database. The C<Component> | ||
| 761 | class is used to manipulate Mason components. | ||
| 762 | |||
| 763 | This rather complicated setup is designed to enable administrators to | ||
| 764 | develop their own report templates. | ||
| 765 | |||
| 766 | |||
| 767 | |||
| 768 | =head1 REPORT GENERATION | ||
| 769 | |||
| 770 | Generation of reports is a core function of any ATT system. This section | ||
| 771 | describes the infrastructure Dochazka provides for this purpose. This | ||
| 772 | infrastructure is built around the L<Mason|https://metacpan.org/pod/Mason> | ||
| 773 | templating system. | ||
| 774 | |||
| 775 | The templates for L<a sample report|/"A typical report"> are provided with | ||
| 776 | the Dochazka distribution. The idea is that site administrators will | ||
| 777 | develop and add more templates to meet their particular reporting needs. | ||
| 778 | |||
| 779 | |||
| 780 | =head2 Infrastructure | ||
| 781 | |||
| 782 | The Dochazka report generation infrastructure has three parts: a template | ||
| 783 | management API, a report population API, and the report generation | ||
| 784 | resource. | ||
| 785 | |||
| 786 | =head3 Template management API | ||
| 787 | |||
| 788 | The template management API is a set of REST resources for creating, | ||
| 789 | reading, updating, and deleting L<Mason|https://metacpan.org/pod/Mason> | ||
| 790 | components. It is built around a "Component" class, instances of which | ||
| 791 | correspond to individual Mason components. | ||
| 792 | |||
| 793 | =head3 Report population API | ||
| 794 | |||
| 795 | The report population API is, basically, the Dochazka data model itself. | ||
| 796 | Since L<Mason|https://metacpan.org/pod/Mason> enables Perl code to be | ||
| 797 | embedded in templates, and since the templates are processed by the | ||
| 798 | Dochazka server, template authors have the entire Dochazka data model at | ||
| 799 | their disposal. | ||
| 800 | |||
| 801 | =head3 Report generation resource | ||
| 802 | |||
| 803 | A REST resource, C<GET genreport>, that takes the path of the Mason component | ||
| 804 | to be run and a hash of arguments to pass to it. The Mason component is run | ||
| 805 | with the provided arguments and the result (a string of characters that can | ||
| 806 | be interpreted as, e.g., an HTML page) is returned in the response content | ||
| 807 | body. | ||
| 808 | |||
| 809 | =back | ||
| 810 | |||
| 811 | |||
| 812 | =head2 Component class | ||
| 813 | |||
| 814 | The C<Component> class is used to work with Mason components. Each instance | ||
| 815 | has the following three attributes: | ||
| 816 | |||
| 817 | =over | ||
| 818 | |||
| 819 | =item Relative path | ||
| 820 | |||
| 821 | The relative path to the component in the Mason component directory tree. | ||
| 822 | |||
| 823 | =item Source code | ||
| 824 | |||
| 825 | The source code of the component, e.g. a mixture of HTML with Mason | ||
| 826 | directives. | ||
| 827 | |||
| 828 | =item ACL profile | ||
| 829 | |||
| 830 | The ACL profile of the component, determining who can use it. As usual, | ||
| 831 | supervisors can generate reports pertaining to employees who report | ||
| 832 | directly to them. | ||
| 833 | |||
| 834 | =back | ||
| 835 | |||
| 836 | For the time being, the C<Component> class does not implement any argument | ||
| 837 | validation. It is up to the caller to provide valid arguments when the | ||
| 838 | component is called. | ||
| 839 | |||
| 840 | |||
| 841 | =head2 A typical report | ||
| 842 | |||
| 843 | Let us examine a typical reporting requirement: a summary of one employee's | ||
| 844 | activity over the course of a time interval. | ||
| 845 | |||
| 846 | In addition to the employee's name, etc., this will require a data set | ||
| 847 | consisting of the days of the month and the total number of hours of each | ||
| 848 | activity logged by (or for) the employee on each day. | ||
| 849 | |||
| 850 | |||
| 851 | |||
| 852 | =head1 CAVEATS | ||
| 853 | |||
| 854 | |||
| 855 | =head2 Unbounded intervals | ||
| 856 | |||
| 857 | Be careful when entering unbounded intervals: PostgreSQL 9.3 is picky about | ||
| 858 | how they are formatted. This, for example, is syntactically correct: | ||
| 859 | |||
| 860 | select * from intervals where intvl && '[,)'; | ||
| 861 | |||
| 862 | But this will generate a syntax error: | ||
| 863 | |||
| 864 | select * from intervals where intvl && '[, )'; | ||
| 865 | |||
| 866 | Even though this is OK: | ||
| 867 | |||
| 868 | select * from intervals where intvl && '[, infinity)'; | ||
| 869 | |||
| 870 | |||
| 871 | =head2 Weekly schedules only | ||
| 872 | |||
| 873 | Unfortunately, the weekly scheduling period is hard-coded at this time. | ||
| 874 | Dochazka does not care what dates are used to define the intervals -- only | ||
| 875 | that they fall within a contiguous 168-hour period. Consider the following | ||
| 876 | contrived example. If the scheduling intervals for EID 1 were defined like | ||
| 877 | this: | ||
| 878 | |||
| 879 | "[1964-12-30 22:05, 1964-12-31 04:35)" | ||
| 880 | "[1964-12-31 23:15, 1965-01-01 03:10)" | ||
| 881 | |||
| 882 | for Dochazka that would mean that the employee with EID 1 has a weekly schedule | ||
| 883 | of "WED/22:05-THU/04:35" and "THU/23:15-FRI/03:10", because the dates in the | ||
| 884 | ranges fall on a Wednesday (1964-12-30), a Thursday (1964-12-31), and a | ||
| 885 | Friday (1964-01-01), respectively. | ||
| 886 | |||
| 887 | |||
| 888 | |||
| 889 | =head2 When history changes take effect | ||
| 890 | |||
| 891 | The C<effective> field of the C<privhistory> and C<schedhistory> tables | ||
| 892 | contains the effective date/time of the history change. This field takes a | ||
| 893 | timestamp, and a trigger ensures that the value is evenly divisible by five | ||
| 894 | minutes (by rounding). In other words, | ||
| 895 | |||
| 896 | '1964-06-13 14:45' | ||
| 897 | |||
| 898 | is a valid C<effective> timestamp, while | ||
| 899 | |||
| 900 | '2014-01-01 00:00:01' | ||
| 901 | |||
| 902 | will be rounded to '2014-01-01 00:00'. | ||
| 903 | |||
| 904 | |||
| 905 | |||
| 906 | =head1 AUTHENTICATION AND SESSION MANAGEMENT | ||
| 907 | |||
| 908 | Employees do not access the database directly, but only via HTTP requests. | ||
| 909 | For authorization and auditing purposes, L<App::Dochazka::REST> needs to | ||
| 910 | associate each incoming request to an EID. | ||
| 911 | |||
| 912 | The L<Plack::Middleware::Session> module associates each incoming request with | ||
| 913 | a session. Sessions are validated by examining the session state in the | ||
| 914 | L<App::Dochazka::REST::Auth> module. | ||
| 915 | |||
| 916 | |||
| 917 | =head2 Existing session | ||
| 918 | |||
| 919 | If the session state is valid, it will contain: | ||
| 920 | |||
| 921 | =over | ||
| 922 | |||
| 923 | =item * the Employee ID, C<eid> | ||
| 924 | |||
| 925 | =item * the IP address from which the session was first originated, C<ip_addr> | ||
| 926 | |||
| 927 | =item * the date/time when the session was last seen, C<last_seen> | ||
| 928 | |||
| 929 | =back | ||
| 930 | |||
| 931 | If any of these are missing, or the difference between C<last_seen> and the | ||
| 932 | current date/time is greater than the time interval defined in the | ||
| 933 | C<DOCHAZKA_REST_SESSION_EXPIRATION_TIME>, the request is rejected with 401 | ||
| 934 | Unauthorized. | ||
| 935 | |||
| 936 | |||
| 937 | =head2 New session | ||
| 938 | |||
| 939 | Requests for a new session are subject to HTTP Basic Authentication. To protect | ||
| 940 | employee credentials from network sniffing attacks, the HTTP traffic | ||
| 941 | must be encrypted. This can be accomplished using an SSL-capable HTTP | ||
| 942 | server or transparent proxy such as L<nginx|http://nginx.org/en/>. | ||
| 943 | |||
| 944 | If the C<DOCHAZKA_LDAP> site parameter is set to a true value, the | ||
| 945 | C<_authenticate> routine of L<App::Dochazka::REST::Resource> will attempt to | ||
| 946 | authenticate the request against an external resource using the LDAP protocol. | ||
| 947 | |||
| 948 | LDAP authentication takes place in two phases: | ||
| 949 | |||
| 950 | =over | ||
| 951 | |||
| 952 | =item * lookup phase | ||
| 953 | |||
| 954 | =item * authentication phase | ||
| 955 | |||
| 956 | =back | ||
| 957 | |||
| 958 | The purpose of the lookup phase is to determine if the user exists in the | ||
| 959 | LDAP resource and, if it does exist, to get its 'cn' property. In the second | ||
| 960 | phase, the password entered by the user is compared with the password stored | ||
| 961 | in the LDAP resource. | ||
| 962 | |||
| 963 | If the LDAP lookup phase fails, or if LDAP is disabled, L<App::Dochazka::REST> | ||
| 964 | falls back to "internal authentication", which means that the credentials are | ||
| 965 | compared against the C<nick>, C<passhash>, and C<salt> fields of the | ||
| 966 | C<employees> table in the database. | ||
| 967 | |||
| 968 | To protect user credentials from snooping, the actual passwords are not stored | ||
| 969 | in the database, Instead, they are run through a one-way hash function and | ||
| 970 | the hash (along with a random "salt" string) is stored in the database instead | ||
| 971 | of the password itself. Since some "one-way" hashing algorithms are subject to | ||
| 972 | brute force attacks, the Blowfish algorithm was chosen to provide the best | ||
| 973 | known protection. | ||
| 974 | |||
| 975 | If the request passes Basic Authentication, a session ID is generated and | ||
| 976 | stored in a cookie. | ||
| 977 | |||
| 978 | |||
| 979 | |||
| 980 | =head1 AUTHORIZATION | ||
| 981 | |||
| 982 | |||
| 983 | |||
| 984 | =head1 CLIENT-SERVER COMMUNICATION | ||
| 985 | |||
| 986 | As stated above, communication between the server and its clients takes place | ||
| 987 | using the HTTP protocol. More abstractly, the communication takes the form of | ||
| 988 | requests (from client to server) and responses (from server back to client) to | ||
| 989 | those requests. In other words, communication is never initiated by the server, | ||
| 990 | but always by the clients. | ||
| 991 | |||
| 992 | |||
| 993 | =head2 HTTP request | ||
| 994 | |||
| 995 | An HTTP request has the following basic components: | ||
| 996 | |||
| 997 | =over | ||
| 998 | |||
| 999 | =item * Method | ||
| 1000 | |||
| 1001 | Dochazka supports GET, PUT, POST, and DELETE | ||
| 1002 | |||
| 1003 | =item * URI | ||
| 1004 | |||
| 1005 | Universal Resource Indicator specifying a Dochazka resource | ||
| 1006 | |||
| 1007 | =item * Headers | ||
| 1008 | |||
| 1009 | More on these below | ||
| 1010 | |||
| 1011 | =item * Request entity | ||
| 1012 | |||
| 1013 | Data accompanying the request - may or may not be present | ||
| 1014 | |||
| 1015 | =back | ||
| 1016 | |||
| 1017 | =head3 Method | ||
| 1018 | |||
| 1019 | The Dochazka REST server accepts the following HTTP methods: | ||
| 1020 | C<GET>, C<PUT>, C<POST>, and C<DELETE>. | ||
| 1021 | |||
| 1022 | =over | ||
| 1023 | |||
| 1024 | =item C<GET> | ||
| 1025 | |||
| 1026 | A C<GET> request on a resource is a request for information - in other words, | ||
| 1027 | it is "read-only": C<GET> requests never change the underlying data. In | ||
| 1028 | Dochazka, C<GET> requests frequently map to C<SELECT> statements. | ||
| 1029 | |||
| 1030 | =item C<PUT> | ||
| 1031 | |||
| 1032 | C<PUT> requests always refer to a concrete data entity, or chunk of data. | ||
| 1033 | In simple cases, this will be a single record in the underlying database. If | ||
| 1034 | the record already exists, the C<PUT> request is interpreted to mean | ||
| 1035 | modification (or C<UPDATE> in SQL). If the record does not exist, then the | ||
| 1036 | request will map to an C<INSERT> statement to create the resource. In both | ||
| 1037 | cases, upon success the response status will be C<200 OK>. | ||
| 1038 | |||
| 1039 | =item C<POST> | ||
| 1040 | |||
| 1041 | Sometimes, especially for create operations, the exact specification of the | ||
| 1042 | resource is not known beforehand. To address these cases, some resources accept | ||
| 1043 | C<POST> requests. If the request causes a new resource to be created, the HTTP | ||
| 1044 | response status will be C<201 Created> and there will be a C<Location> header | ||
| 1045 | specifying the URI of the newly created resource. | ||
| 1046 | |||
| 1047 | =item C<DELETE> | ||
| 1048 | |||
| 1049 | As their name would suggest, C<DELETE> requests are issued when we want to | ||
| 1050 | dissolve (destroy) a resource. Whether or not this actually happens is | ||
| 1051 | determined by two factors: (1) whether the user issuing the request has the | ||
| 1052 | requisite authorization and, (2), whether the underlying data record is | ||
| 1053 | referred to by other records - in which case typically the C<DELETE> request | ||
| 1054 | will fail with a C<500 Internal Server Error> status. | ||
| 1055 | |||
| 1056 | =back | ||
| 1057 | |||
| 1058 | =head3 URI | ||
| 1059 | |||
| 1060 | The purpose of the Universal Resource Indicator (URI, sometimes also known as | ||
| 1061 | an URL) is to uniquely identify a resource. | ||
| 1062 | |||
| 1063 | URIs consist of several syntactical elements. An exhaustive description can be | ||
| 1064 | found in RFC ..., but for Dochazka purposes we can present them as follows: | ||
| 1065 | |||
| 1066 | =over | ||
| 1067 | |||
| 1068 | =item C<https://> | ||
| 1069 | |||
| 1070 | This part of the URI says that we are using the HTTPS protocol (or SSL-encrypted | ||
| 1071 | HTTP) to communicate. It is separated from the next component by two forward | ||
| 1072 | slashes. | ||
| 1073 | |||
| 1074 | =item C<dochazka.site> | ||
| 1075 | |||
| 1076 | After the protocol, the next URI component is the REST server's domain name. | ||
| 1077 | Obviously, this will differ from site to site. It is separated from the next | ||
| 1078 | component (i.e. the resource specification) by a single forward slash. | ||
| 1079 | |||
| 1080 | =item Dochazka resource | ||
| 1081 | |||
| 1082 | As stated above, the domain name is terminated by a single forward slash. | ||
| 1083 | Everything after that is interpreted as a resource specification. | ||
| 1084 | |||
| 1085 | A single forward slash C<'/'> specifies the root resource. | ||
| 1086 | |||
| 1087 | =back | ||
| 1088 | |||
| 1089 | Of these three components, the first two are site-specific. It is possible, | ||
| 1090 | for example, to run the Dochazka server without SSL encryption, in which case | ||
| 1091 | the protocol would be C<http://> instead of C<https://>. | ||
| 1092 | |||
| 1093 | Once the application's implementation at a given site has stabilized, these two | ||
| 1094 | URI components will change very seldomly, if at all. | ||
| 1095 | |||
| 1096 | Dochazka resources are much more ephemeral. Different resources present | ||
| 1097 | different ways that users can access and modify the data (in this case, | ||
| 1098 | attendance data) in the underlying database. | ||
| 1099 | |||
| 1100 | Some resources, such as C<employee/nick/simona>, refer directly to a unit of | ||
| 1101 | information that may or may not exist in the database information. Other | ||
| 1102 | resources, like C<interval/new>, are not linked to a specific database record. | ||
| 1103 | |||
| 1104 | Also, in programming terms the resources are generalized, so we think about, | ||
| 1105 | e.g., C<employee/nick/simona> and C<employee/nick/wanda> as two instances of a | ||
| 1106 | more generalized C<employee/nick/:nick> resource, where C<:nick> is like an | ||
| 1107 | argument to a function call. | ||
| 1108 | |||
| 1109 | And, indeed, internally all resources resolve to function calls. The function | ||
| 1110 | in this case is referred to as the "resource handler". | ||
| 1111 | |||
| 1112 | Some resources accept all four HTTP methods listed above, others accept two or | ||
| 1113 | three, and still others accept only one. | ||
| 1114 | |||
| 1115 | =head3 Headers | ||
| 1116 | |||
| 1117 | HTTP headers are somewhat obscure because they are often hidden by the | ||
| 1118 | client. Nevertheless, they are an important part of the HTTP protocol. The | ||
| 1119 | Dochazka REST server only accepts certain headers in the request. | ||
| 1120 | |||
| 1121 | #FIXME: describe the more common response headers | ||
| 1122 | |||
| 1123 | =head3 Body | ||
| 1124 | |||
| 1125 | C<PUT> and C<POST> requests may take a request body. If a request body is | ||
| 1126 | expected or accepted, it must be a valid JSON string. (JSON is a simple way of | ||
| 1127 | "stringifying" a data structure.) | ||
| 1128 | |||
| 1129 | =head2 HTTP response | ||
| 1130 | |||
| 1131 | The HTTP response returned by the REST server consists of: | ||
| 1132 | |||
| 1133 | =over | ||
| 1134 | |||
| 1135 | =item * Status code (e.g. 200, 400, 404, etc.) | ||
| 1136 | |||
| 1137 | =item * Headers | ||
| 1138 | |||
| 1139 | =item * Content body (or "response entity") | ||
| 1140 | |||
| 1141 | =back | ||
| 1142 | |||
| 1143 | =head3 Status | ||
| 1144 | |||
| 1145 | The HTTP standard stipulates a number of status codes. The server listens for | ||
| 1146 | incoming requests. Under normal operation, the server processes each request. | ||
| 1147 | The result of such processing is a "response", which is sent back to the client | ||
| 1148 | that originated the request. Each response will contain one and only one status | ||
| 1149 | code. The meanings of the various status codes are explained in the HTTP | ||
| 1150 | standard. Some of the more common ones are as follows: | ||
| 1151 | |||
| 1152 | =over | ||
| 1153 | |||
| 1154 | =item C<200> (OK) | ||
| 1155 | |||
| 1156 | The request was accepted and processed. Refer to the response body for the | ||
| 1157 | result. | ||
| 1158 | |||
| 1159 | =item C<204> () | ||
| 1160 | |||
| 1161 | This code is returned on C<DELETE> requests when either the record was | ||
| 1162 | successfully deleted or the resource did not exist in the first place. | ||
| 1163 | |||
| 1164 | =item C<404> (Not Found) | ||
| 1165 | |||
| 1166 | The resource specification given in the URI could not be associated with a | ||
| 1167 | known resource. | ||
| 1168 | |||
| 1169 | =item C<405> (Method Not Allowed) | ||
| 1170 | |||
| 1171 | The resource was recognized but it is not defined for this method. | ||
| 1172 | |||
| 1173 | =item C<401> (Not authorized) | ||
| 1174 | |||
| 1175 | A valid method+resource combination was specified, but the user failed to | ||
| 1176 | authenticate herself to the REST server. | ||
| 1177 | |||
| 1178 | =item C<403> (Forbidden) | ||
| 1179 | |||
| 1180 | A valid method+resource combination was specified and the user was successfully | ||
| 1181 | authenticated, but the user is not authorized to perform the operation she is | ||
| 1182 | requesting. | ||
| 1183 | |||
| 1184 | =item C<400> (Malformed) | ||
| 1185 | |||
| 1186 | A valid method+resource combination was specified and the user passed | ||
| 1187 | authentication and authorization. However, the information provided by the user | ||
| 1188 | in the resource specification or in the request body could not be parsed. | ||
| 1189 | |||
| 1190 | =back | ||
| 1191 | |||
| 1192 | =head3 Headers | ||
| 1193 | |||
| 1194 | HTTP responses are always accompanied by headers, which qualify the response in | ||
| 1195 | various ways. For example, the C<Content-Encoding> header indicates how the | ||
| 1196 | bytes in the response body string should be interpreted. In Dochazka's case, the | ||
| 1197 | response body will always be in JSON, which implies the C<UTF-8> encoding. | ||
| 1198 | |||
| 1199 | #FIXME: describe the more common response headers | ||
| 1200 | |||
| 1201 | =head3 Body | ||
| 1202 | |||
| 1203 | The response body holds the information returned by a "successful" request. Be | ||
| 1204 | aware that "success" in this context only means that, from the perspective of | ||
| 1205 | the REST server, the request was fully processed. It does I<not> means that | ||
| 1206 | that whatever the user was requesting was actually done. For example, if a | ||
| 1207 | C<GET> request for a resource is sent and the reponse code is 200, the client | ||
| 1208 | programmer can assume that a response body will be present, and that it will | ||
| 1209 | be an L<App::CELL::Status> object, but that is all she can assume. | ||
| 1210 | Particularly, she cannot assume that the payload of that status object | ||
| 1211 | contains the object requested. | ||
| 1212 | |||
| 1213 | As stated above, the response body will always be a JSON string. This string | ||
| 1214 | can either be displayed to the user as-is, or it can be interpreted and further | ||
| 1215 | processed by the client. | ||
| 1216 | |||
| 1217 | A body may be included in the response, provided the status code is C<200> (OK). | ||
| 1218 | For the client, an HTTP response of 200 is a signal to look into the response | ||
| 1219 | body for further information. For the purposes of Dochazka, a 200 status does | ||
| 1220 | not provide any information beyond this imperative: "look into the response | ||
| 1221 | body". | ||
| 1222 | |||
| 1223 | Since looking into the response body is fundamental to the operation of | ||
| 1224 | Dochazka clients, the REST server emits response bodies in a strictly defined | ||
| 1225 | format detailed in L<App::CELL::Status>. Client developers, then, can count | ||
| 1226 | on the truthfulness of the following statements: | ||
| 1227 | |||
| 1228 | =over | ||
| 1229 | |||
| 1230 | =item "A response body will be sent if, and only if, the HTTP status code is 200" | ||
| 1231 | |||
| 1232 | =item "The response body will always be a JSON string" | ||
| 1233 | |||
| 1234 | =item "That JSON string will always be an App::CELL::Status object" | ||
| 1235 | |||
| 1236 | =back | ||
| 1237 | |||
| 1238 | While the HTTP protocol provides a range of general status codes, some of which | ||
| 1239 | make sense for and are used by Dochazka, in some cases Dochazka needs to return | ||
| 1240 | status codes that are not provided for by the standard. For this reason, the | ||
| 1241 | information returned by Dochazka is further encapsulated in a Dochazka-specific | ||
| 1242 | status structure, and this is what is returned in the response body. | ||
| 1243 | |||
| 1244 | |||
| 1245 | |||
| 1246 | =head1 DATABASE | ||
| 1247 | |||
| 1248 | The "state" of practically all data model objects is stored in a PostgreSQL | ||
| 1249 | database - one database per site. | ||
| 1250 | |||
| 1251 | PostgreSQL is used for its tsrange type (see | ||
| 1252 | L<http://www.postgresql.org/docs/9.4/static/rangetypes.html> for details), | ||
| 1253 | which is used to store and manipulate schedule and attendance intervals, as | ||
| 1254 | well as locks. | ||
| 1255 | |||
| 1256 | The database is accessed through L<DBI>, and the L<DBI> connection is | ||
| 1257 | accessed through a L<DBIx::Connector> singleton stored in | ||
| 1258 | L<App::Dochazka::Rest::ConnBank>. When an incoming request is | ||
| 1259 | authenticated, the L<DBIx::Connector> singleton is placed in the request | ||
| 1260 | context (a hashref used to store request processing state). | ||
| 1261 | |||
| 1262 | The purpose of this somewhat complicated mechanism is to ensure that each | ||
| 1263 | request can potentially have its own database connection. | ||
| 1264 | |||
| 1265 | |||
| 1266 | |||
| 1267 | =head1 DEBUGGING | ||
| 1268 | |||
| 1269 | L<App::Dochazka::REST> offers the following debug facilities: | ||
| 1270 | |||
| 1271 | =over | ||
| 1272 | |||
| 1273 | =item * C<bin/dochazka-rest -- --early-debug=$TMPFILE> | ||
| 1274 | |||
| 1275 | Calling the server startup script as shown will cause the C<--early-debug> | ||
| 1276 | parameter to be passed through to C<bin/mrest> (from the L<Web::MREST> distro). | ||
| 1277 | This has the interesting effect of capturing even the earliest debug messages | ||
| 1278 | to a temporary file, which the server must be able to C<touch>. Once the server | ||
| 1279 | has started, the filename can also be determined by sending a C<GET | ||
| 1280 | param/meta/MREST_EARLY_DEBUGGING> request. | ||
| 1281 | |||
| 1282 | =item * DOCHAZKA_DEBUG environment variable | ||
| 1283 | |||
| 1284 | If the C<DOCHAZKA_DEBUG> environment variable is set to a true value, the | ||
| 1285 | entire 'context' will be returned in each JSON response, instead of just | ||
| 1286 | the 'entity'. For more information, see C<Resource.pm>. | ||
| 1287 | |||
| 1288 | =item * MREST_DEBUG_MODE site configuration parameter | ||
| 1289 | |||
| 1290 | If the C<MREST_DEBUG_MODE> site parameter is set to a true value, | ||
| 1291 | debug messages will be logged. | ||
| 1292 | |||
| 1293 | =back | ||
| 1294 | |||
| 1295 | |||
| 1296 | =head1 GLOSSARY OF TERMS | ||
| 1297 | |||
| 1298 | In Dochazka, some commonly-used terms have special meanings: | ||
| 1299 | |||
| 1300 | =over | ||
| 1301 | |||
| 1302 | =item * B<employee> -- | ||
| 1303 | Regardless of whether they are employees in reality, for the | ||
| 1304 | purposes of Dochazka employees are the folks whose attendance/time is being | ||
| 1305 | tracked. Employees are expected to interact with Dochazka using the | ||
| 1306 | following functions and commands. | ||
| 1307 | |||
| 1308 | =item * B<administrator> -- | ||
| 1309 | In Dochazka, administrators are employees with special powers. Certain | ||
| 1310 | REST/CLI functions are available only to administrators. | ||
| 1311 | |||
| 1312 | =item * B<CLI client> -- | ||
| 1313 | CLI stands for Command-Line Interface. The CLI client is the Perl script | ||
| 1314 | that is run when an employee types C<dochazka> at the bash prompt. | ||
| 1315 | |||
| 1316 | =item * B<REST server> -- | ||
| 1317 | REST stands for ... . The REST server is a collection of Perl modules | ||
| 1318 | running on a server at the site. | ||
| 1319 | |||
| 1320 | =item * B<site> -- | ||
| 1321 | In a general sense, the "site" is the company, organization, or place that | ||
| 1322 | has implemented (installed, configured) Dochazka for attendance/time | ||
| 1323 | tracking. In a technical sense, a site is a specific instance of the | ||
| 1324 | Dochazka REST server that CLI clients connect to. | ||
| 1325 | |||
| 1326 | =back | ||
| 1327 | |||
| 1328 | |||
| 1329 | |||
| 1330 | =head1 AUTHOR | ||
| 1331 | |||
| 1332 | Nathan Cutler, C<< <ncutler@suse.cz> >> | ||
| 1333 | |||
| 1334 | |||
| 1335 | |||
| 1336 | |||
| 1337 | =head1 BUGS | ||
| 1338 | |||
| 1339 | To report bugs or request features, use the GitHub issue tracker at | ||
| 1340 | L<https://github.com/smithfarm/dochazka-rest/issues>. | ||
| 1341 | |||
| 1342 | |||
| 1343 | |||
| 1344 | |||
| 1345 | =head1 LICENSE AND COPYRIGHT | ||
| 1346 | |||
| 1347 | Copyright (c) 2014-2015, SUSE LLC | ||
| 1348 | All rights reserved. | ||
| 1349 | |||
| 1350 | Redistribution and use in source and binary forms, with or without | ||
| 1351 | modification, are permitted provided that the following conditions are met: | ||
| 1352 | |||
| 1353 | 1. Redistributions of source code must retain the above copyright notice, this | ||
| 1354 | list of conditions and the following disclaimer. | ||
| 1355 | |||
| 1356 | 2. Redistributions in binary form must reproduce the above copyright notice, | ||
| 1357 | this list of conditions and the following disclaimer in the documentation | ||
| 1358 | and/or other materials provided with the distribution. | ||
| 1359 | |||
| 1360 | 3. Neither the name of SUSE LLC nor the names of its contributors | ||
| 1361 | may be used to endorse or promote products derived from this software without | ||
| 1362 | specific prior written permission. | ||
| 1363 | |||
| 1364 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
| 1365 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
| 1366 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| 1367 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
| 1368 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 1369 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| 1370 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| 1371 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
| 1372 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| 1373 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 1374 | |||
| 1375 | |||
| 1376 | =cut | ||
| 1377 | |||
| 1378 | 1; # End of App::Dochazka::REST::Guide |