template-instantiate-user-attributes.patch

Simon Boulet, 12/17/2012 05:03 PM

Download (5.67 KB)

View differences:

opennebula-3.8.1/include/RequestManagerVMTemplate.h 2012-12-16 16:30:54.000000000 -0500
57 57
    VMTemplateInstantiate():
58 58
        RequestManagerVMTemplate("TemplateInstantiate",
59 59
                                 "Instantiates a new virtual machine using a template",
60
                                 "A:sis")
60
                                 "A:siss")
61 61
    {
62 62
        auth_op = AuthRequest::USE;
63 63
    };
opennebula-3.8.1/include/Template.h 2012-12-16 19:47:55.000000000 -0500
283 283
     */
284 284
    int from_xml_node(const xmlNodePtr node);
285 285

  
286
    /**
287
     *  Merge attributes from another template
288
     *    @param from_tmpl the template to be merged
289
     *    @param error_str string describing the error
290
     *    @return 0 on success.
291
     */
292
     int merge(const Template * from_tmpl, string& error_str);
293

  
286 294
protected:
287 295
    /**
288 296
     *  The template attributes
opennebula-3.8.1/src/rm/RequestManagerVMTemplate.cc 2012-12-17 00:20:18.000000000 -0500
27 27
    int    id   = xmlrpc_c::value_int(paramList.getInt(1));
28 28
    string name = xmlrpc_c::value_string(paramList.getString(2));
29 29

  
30
    // optional parameter
31
    string str_uattrs;
32
    try {
33
        str_uattrs = xmlrpc_c::value_string(paramList.getString(3));
34
    } catch (...) { }
35

  
30 36
    int rc, vid;
31 37

  
32 38
    PoolObjectAuth perms;
......
38 44
    VirtualMachineTemplate * tmpl;
39 45
    VMTemplate *             rtmpl;
40 46

  
47
    VirtualMachineTemplate uattrs;
48

  
41 49
    string error_str;
42 50
    string aname;
43 51

  
......
58 66

  
59 67
    rtmpl->unlock();
60 68

  
69
    // Parse user supplied attributes
70
    rc = uattrs.parse_str_or_xml(str_uattrs, error_str);
71

  
72
    if ( rc != 0 )
73
    {
74
        failure_response(INTERNAL, error_str, att);
75
        delete tmpl;
76

  
77
        return;
78
    }
79

  
61 80
    // Check template for restricted attributes, but only if the Template owner
62 81
    // is not oneadmin
63 82

  
......
78 97
        }
79 98
    }
80 99

  
100
    // Check user attributes for restricted attributes, but only if the Request user
101
    // is not oneadmin
102

  
103
    if ( att.uid != UserPool::ONEADMIN_ID && att.gid != GroupPool::ONEADMIN_ID )
104
    {
105
        if (uattrs.check(aname))
106
        {
107
            ostringstream oss;
108

  
109
            oss << "User Attributes includes a restricted attribute " << aname;
110

  
111
            failure_response(AUTHORIZATION,
112
                    authorization_error(oss.str(), att),
113
                    att);
114

  
115
            delete tmpl;
116
            return;
117
        }
118
    }
119

  
120
    // Merge user attributes into template
121
    rc = tmpl->merge(&uattrs, error_str);
122

  
123
    if ( rc != 0 )
124
    {
125
        failure_response(INTERNAL, error_str, att);
126
        delete tmpl;
127

  
128
        return;
129
    }
130

  
81 131
    tmpl->erase("NAME");
82 132
    tmpl->set(new SingleAttribute("NAME",name));
83 133

  
opennebula-3.8.1/src/template/Template.cc 2012-12-17 00:09:24.000000000 -0500
635 635
/* ------------------------------------------------------------------------ */
636 636
/* ------------------------------------------------------------------------ */
637 637

  
638
int Template::merge(const Template * from_tmpl, string& error_str)
639
{
640
    multimap<string,Attribute *>::const_iterator it;
641

  
642
    for (it = from_tmpl->attributes.begin(); it != from_tmpl->attributes.end(); ++it) {
643

  
644
        // Get the attribute to be replaced
645
        vector<Attribute*> attrs;
646
        get(it->first, attrs);
647

  
648
        // Insert if attribute does not exist
649
        if ( attrs.size() == 0 )
650
        {
651
            attributes.insert(make_pair(it->first,(it->second)->clone()));
652
            continue;
653
        }
654

  
655
        Attribute* attr = attrs[0];
656

  
657
        if (attr->type() != it->second->type())
658
        {
659
            ostringstream oss;
660
            oss << "Cannot merge attributes of different types " << it->first;
661
            error_str = oss.str();
662
            return -1;
663
        }
664
   
665
        if (attr->type() == Attribute::SIMPLE)
666
        {
667
            // Replace an existing Simple Attribute
668
            ((SingleAttribute*) attr)->replace( ((SingleAttribute*) it->second)->value());
669
            continue;
670
        }
671
        else
672
        {
673
            // Replace values of an existing Vector Attribute
674
            const map<string,string> values = ((VectorAttribute*) it->second)->value();
675

  
676
            map<string,string>::const_iterator it;
677

  
678
            for (it = values.begin(); it != values.end(); ++it)
679
            {
680
                ((VectorAttribute*) attr)->replace(it->first, it->second);
681
            }
682
        }
683
    }
684

  
685
    return 0;
686
}
687

  
688
/* ------------------------------------------------------------------------ */
689
/* ------------------------------------------------------------------------ */
690

  
638 691
void Template::rebuild_attributes(const xmlNode * root_element)
639 692
{
640 693
    xmlNode * cur_node = 0;